Le blog de Fabien DUMINY

Un blog utilisant WordPress

Skip to: Content | Sidebar | Footer

Processing an HTTP response with the apache client

24 April, 2012 (09:00) | Java | By: fabien

As I had to do an HTTP request under android, I started to write the following code using the apache HTTP client API (included in android) :

// create and initialize the apache http client
final HttpClient httpClient = ......;

// create and initialize the http request
HttpPost httppost = ......; 

ResponseHandler rh = new ResponseHandler() {
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            StringBuilder out = new StringBuilder();
            byte[] b = EntityUtils.toByteArray(entity);
            out.append(new String(b, 0, b.length));
            return out.toString();
        } else {
            return "";
        }
    }
};
String responseString = httpClient.execute(httppost, rh);

After a problem, I discovered the server returned an http error 403 (access forbidden). So, I added the highlighted code below in order to manage the case it doesn’t return OK :

// create and initialize the apache http client
final HttpClient httpClient = ......;

// create and initialize the http request
HttpPost httppost = ......; 

ResponseHandler rh = new ResponseHandler() {
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
        if (response.getStatusLine().getStatusCode()
                                          == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                StringBuilder out = new StringBuilder();
                byte[] b = EntityUtils.toByteArray(entity);
                out.append(new String(b, 0, b.length));
                return out.toString();
            } else {
                return "";
            }
        } else {
            StatusLine sl = response.getStatusLine();
            String message = "HTTP error " + sl.getStatusCode() +
                             " Reason: " + sl.getReasonPhrase();
            throw new HttpErrorException(message);
        }
    }
};
String responseString = httpClient.execute(httppost, rh);

remark : I have created the HttpErrorException class that inherits from IOException to stay compliant with the handleResponse method specifications from the ResponseHandler interface.
After that modification, the caller must catch HttpErrorException to process specifically the case when the server doesn’t return OK.

Be Sociable, Share!
Share and Enjoy

Comments

avatar

Comment from Craig
Time %A %B %e%q, %Y at %I:%M %p

Google has deprecated (and essentially abandoned, as they won’t be updating it from upstream ever again) HttpClient: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
With that in mind, I’d really think about not using it.

Write a comment





If your website is claim enabled, it will be notified that you have posted here.

MySQL query error