Example usage for org.apache.http.client ClientProtocolException getMessage

List of usage examples for org.apache.http.client ClientProtocolException getMessage

Introduction

In this page you can find the example usage for org.apache.http.client ClientProtocolException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.smilec.smile.util.HttpUtil.java

private static final InputStream executeMethod(HttpUriRequest request) throws NetworkErrorException {

    HttpClient client = new DefaultHttpClient();

    // Execute/*from   w  w w  .  j a v a 2s.  co m*/
    HttpResponse response = null;
    try {
        response = client.execute(request);
    } catch (ClientProtocolException e) {
        throw new NetworkErrorException("Unexpected error executing request: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new NetworkErrorException("Unexpected error request: " + e.getMessage(), e);
    }

    // Check the status code
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();

    try {
        // Returning the content
        HttpEntity entity = response.getEntity();

        if (statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR || statusCode == HttpStatus.SC_NOT_FOUND) {
            JSONObject json = new JSONObject(EntityUtils.toString(entity));
            String message = json.getString("message");
            throw new NetworkErrorException(message);
        }

        if (statusCode != HttpStatus.SC_OK) {
            throw new NetworkErrorException("Unexpected HTTP Status Code: " + statusCode);
        }

        return entity.getContent();
    } catch (IllegalStateException e) {
        throw new NetworkErrorException("Unexpected error returnig the request content: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new NetworkErrorException("Unexpected error returnig the request content: " + e.getMessage(), e);
    } catch (ParseException e) {
        throw new NetworkErrorException(e.getMessage());
    } catch (JSONException e) {
        throw new NetworkErrorException(e.getMessage());
    }
}