Back to project page cloudmine-android.
The source code is released under:
Copyright (c) 2012 CloudMine LLC, http://cloudmine.me Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software")...
If you think the Android project cloudmine-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Android Asynchronous Http Client// w ww .jav a2 s. c o m Copyright (c) 2011 James Smith <james@loopj.com> http://loopj.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ****This file has been modified from its original version by CloudMine LLC***** */ package com.cloudmine.api.loopj; import com.cloudmine.api.rest.ResponseTimeDataStore; import com.cloudmine.api.rest.callbacks.Callback; import org.apache.http.HttpResponse; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.AbstractHttpClient; import org.apache.http.protocol.HttpContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.ConnectException; class AsyncHttpRequest implements Runnable { private static final Logger LOG = LoggerFactory.getLogger(AsyncHttpRequest.class); private final AbstractHttpClient client; private final HttpContext context; private final HttpUriRequest request; private final AsyncHttpResponseHandler responseHandler; private int executionCount; public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, AsyncHttpResponseHandler responseHandler) { this.client = client; this.context = context; this.request = request; this.responseHandler = responseHandler; } public void run() { try { if(responseHandler != null){ responseHandler.sendStartMessage(); } makeRequestWithRetries(); if(responseHandler != null) { responseHandler.sendFinishMessage(); } } catch (IOException e) { if(responseHandler != null) { responseHandler.sendFinishMessage(); responseHandler.sendFailureMessage(e, null); } } } private void makeRequest() throws IOException { try { if(!Thread.currentThread().isInterrupted()) { HttpResponse response = client.execute(request, context); if(!Thread.currentThread().isInterrupted()) { if(responseHandler != null) { if(responseHandler instanceof Callback) { ResponseTimeDataStore.extractAndStoreResponseTimeInformation((Callback) responseHandler, response); } else { LOG.error("We have a non callback responseHandler; this should never happen. Not storing response time information"); } responseHandler.sendResponseMessage(response); } } else{ //TODO: should raise InterruptedException? this block is reached whenever the request is cancelled before its response is received } } }catch (Throwable t) { if(t instanceof IOException) throw (IOException)t; else responseHandler.handleFailureMessage(t, "Error while making request"); } } private void makeRequestWithRetries() throws ConnectException { // This is an additional layer of retry logic lifted from droid-fu // See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java boolean retry = true; IOException cause = null; HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler(); while (retry) { try { makeRequest(); return; } catch (IOException e) { cause = e; retry = retryHandler.retryRequest(cause, ++executionCount, context); } catch (NullPointerException e) { // there's a bug in HttpClient 4.0.x that on some occasions causes // DefaultRequestExecutor to throw an NPE, see // http://code.google.com/p/android/issues/detail?id=5255 cause = new IOException("NPE in HttpClient" + e.getMessage()); retry = retryHandler.retryRequest(cause, ++executionCount, context); } } // no retries left, crap out with exception ConnectException ex = new ConnectException(); ex.initCause(cause); throw ex; } }