Example usage for org.apache.http.conn ConnectTimeoutException getCause

List of usage examples for org.apache.http.conn ConnectTimeoutException getCause

Introduction

In this page you can find the example usage for org.apache.http.conn ConnectTimeoutException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.iia.giveastick.util.RestClient.java

public String invoke(Verb verb, String path, JSONObject jsonParams, List<Header> headers) throws Exception {
    //EncodingTypes charset=serviceContext.getCharset();
    long startTime = 0, endTime = 0;
    String result = null;//from  ww w  .j av a2 s  .  co m
    this.statusCode = 404;

    HttpResponse response = null;
    HttpHost targetHost = new HttpHost(this.serviceName, this.port, this.scheme);

    //HttpEntity entity = this.buildMultipartEntity(params);
    HttpEntity entity = null;

    if (jsonParams != null && jsonParams.has("file")) {
        entity = this.buildMultipartEntity(jsonParams);
    } else {
        entity = this.buildJsonEntity(jsonParams);
    }

    HttpRequest request = this.buildHttpRequest(verb, path, entity, headers);

    if (this.login != null && this.password != null) {
        request.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(this.login, this.password),
                "UTF-8", false));
    }

    if (!TextUtils.isEmpty(this.CSRFtoken)) {
        request.addHeader("X-CSRF-Token", this.CSRFtoken);
    }

    try {
        if (debugWeb) {
            startTime = System.currentTimeMillis();
        }

        response = this.httpClient.execute(targetHost, request);
        this.statusCode = response.getStatusLine().getStatusCode();

        this.readHeader(response);

        if (debugWeb) {
            endTime = System.currentTimeMillis();
        }

        // we assume that the response body contains the error message
        HttpEntity resultEntity = response.getEntity();

        if (resultEntity != null) {
            result = EntityUtils.toString(resultEntity, HTTP.UTF_8);
        }

        if (debugWeb && GiveastickApplication.DEBUG) {
            final long endTime2 = System.currentTimeMillis();

            //System.out.println(
            //   "The REST response is:\n " + serviceResponse);
            Log.d(TAG, "Time taken in REST operation : " + (endTime - startTime) + " ms. => [" + verb + "]"
                    + path);
            Log.d(TAG, "Time taken in service response construction : " + (endTime2 - endTime) + " ms.");
        }

    } catch (ConnectTimeoutException e) {
        Log.e(TAG, "Connection timed out. The host may be unreachable.");
        e.printStackTrace();

        throw new Exception(e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, e.getCause().getMessage());

        throw new Exception(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();

        throw e;
    } finally {
        this.httpClient.getConnectionManager().shutdown();
    }

    return result;
}