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

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.android.providers.downloads.OmaDownload.java

/**
* This method notifies the server for the status of the download operation.
* It sends a status report to a Web server if installNotify attribute is specified in the download descriptor.
@param  component   the component that contains attributes in the descriptor.
@param  handler     the handler used to send and process messages. A message
                indicates whether the media object is available to the user
                or not (READY or DISCARD).
*//*from  w w w. j  a  va 2  s  .  c o  m*/
//protected static void installNotify (OmaDescription component, Handler handler) {
protected static int installNotify(OmaDescription component, Handler handler) {

    int ack = -1;
    int release = OmaStatusHandler.DISCARD;
    URL url = component.getInstallNotifyUrl();

    if (url != null) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(url.toString());

        try {
            HttpParams params = postRequest.getParams();
            HttpProtocolParams.setUseExpectContinue(params, false);
            postRequest.setEntity(
                    new StringEntity(OmaStatusHandler.statusCodeToString(component.getStatusCode()) + "\n\r"));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                // TODO Auto-generated method stub
                Log.i("@M_" + Constants.LOG_OMA_DL, "Retry the request...");
                return (executionCount <= OmaStatusHandler.MAXIMUM_RETRY);
            }
        });

        try {
            HttpResponse response = client.execute(postRequest);

            if (response.getStatusLine() != null) {
                ack = response.getStatusLine().getStatusCode();

                //200-series response code
                if (ack == HttpStatus.SC_OK || ack == HttpStatus.SC_ACCEPTED || ack == HttpStatus.SC_CREATED
                        || ack == HttpStatus.SC_MULTI_STATUS
                        || ack == HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION || ack == HttpStatus.SC_NO_CONTENT
                        || ack == HttpStatus.SC_PARTIAL_CONTENT || ack == HttpStatus.SC_RESET_CONTENT) {
                    if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
                        release = OmaStatusHandler.READY;
                    }
                }

                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        inputStream = entity.getContent();
                        if (inputStream != null) {
                            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

                            String s;
                            while ((s = br.readLine()) != null) {
                                Log.v("@M_" + Constants.LOG_OMA_DL, "Response: " + s);
                            }
                        }

                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
            }
        } catch (ConnectTimeoutException e) {
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
            postRequest.abort();
            //After time out period, the client releases the media object for use.
            if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
                release = OmaStatusHandler.READY;
            }
        } catch (NoHttpResponseException e) {
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
            postRequest.abort();
            //After time out period, the client releases the media object for use.
            if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
                release = OmaStatusHandler.READY;
            }
        } catch (IOException e) {
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
            postRequest.abort();
        }
        if (client != null) {
            client.getConnectionManager().shutdown();
        }
    } else {
        if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
            release = OmaStatusHandler.READY;
        }
    }

    if (handler != null) {
        Message mg = Message.obtain();
        mg.arg1 = release;
        handler.sendMessage(mg);
    }
    return release;
}