18
18
19
19
package com .loopj .android .http ;
20
20
21
+ import com .loopj .android .http .handlers .RangeFileAsyncHttpResponseHandler ;
22
+ import com .loopj .android .http .interfaces .ResponseHandlerInterface ;
23
+ import com .loopj .android .http .utils .Utils ;
24
+
21
25
import java .io .IOException ;
22
26
import java .net .MalformedURLException ;
23
- import java .net .UnknownHostException ;
24
27
import java .util .concurrent .atomic .AtomicBoolean ;
25
28
26
29
import cz .msebera .android .httpclient .HttpResponse ;
27
- import cz .msebera .android .httpclient .client .HttpRequestRetryHandler ;
28
30
import cz .msebera .android .httpclient .client .methods .HttpUriRequest ;
29
- import cz .msebera .android .httpclient .impl .client .AbstractHttpClient ;
30
- import cz .msebera .android .httpclient .protocol .HttpContext ;
31
+ import cz .msebera .android .httpclient .impl .client .CloseableHttpClient ;
31
32
32
33
/**
33
34
* Internal class, representing the HttpRequest, done in asynchronous manner
34
35
*/
35
36
public class AsyncHttpRequest implements Runnable {
36
- private final AbstractHttpClient client ;
37
- private final HttpContext context ;
37
+ private final CloseableHttpClient client ;
38
38
private final HttpUriRequest request ;
39
39
private final ResponseHandlerInterface responseHandler ;
40
40
private final AtomicBoolean isCancelled = new AtomicBoolean ();
@@ -43,9 +43,8 @@ public class AsyncHttpRequest implements Runnable {
43
43
private volatile boolean isFinished ;
44
44
private boolean isRequestPreProcessed ;
45
45
46
- public AsyncHttpRequest (AbstractHttpClient client , HttpContext context , HttpUriRequest request , ResponseHandlerInterface responseHandler ) {
47
- this .client = Utils .notNull (client , "client" );
48
- this .context = Utils .notNull (context , "context" );
46
+ public AsyncHttpRequest (CloseableHttpClient httpClient , HttpUriRequest request , ResponseHandlerInterface responseHandler ) {
47
+ this .client = Utils .notNull (httpClient , "client" );
49
48
this .request = Utils .notNull (request , "request" );
50
49
this .responseHandler = Utils .notNull (responseHandler , "responseHandler" );
51
50
}
@@ -103,7 +102,7 @@ public void run() {
103
102
}
104
103
105
104
try {
106
- makeRequestWithRetries ();
105
+ makeRequest ();
107
106
} catch (IOException e ) {
108
107
if (!isCancelled ()) {
109
108
responseHandler .sendFailureMessage (0 , null , null , e );
@@ -143,7 +142,7 @@ private void makeRequest() throws IOException {
143
142
((RangeFileAsyncHttpResponseHandler ) responseHandler ).updateRequestHeaders (request );
144
143
}
145
144
146
- HttpResponse response = client .execute (request , context );
145
+ HttpResponse response = client .execute (request );
147
146
148
147
if (isCancelled ()) {
149
148
return ;
@@ -167,49 +166,6 @@ private void makeRequest() throws IOException {
167
166
responseHandler .onPostProcessResponse (responseHandler , response );
168
167
}
169
168
170
- private void makeRequestWithRetries () throws IOException {
171
- boolean retry = true ;
172
- IOException cause = null ;
173
- HttpRequestRetryHandler retryHandler = client .getHttpRequestRetryHandler ();
174
- try {
175
- while (retry ) {
176
- try {
177
- makeRequest ();
178
- return ;
179
- } catch (UnknownHostException e ) {
180
- // switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
181
- // while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
182
- // (to assist in genuine cases of unknown host) which seems better than outright failure
183
- cause = new IOException ("UnknownHostException exception: " + e .getMessage ());
184
- retry = (executionCount > 0 ) && retryHandler .retryRequest (e , ++executionCount , context );
185
- } catch (NullPointerException e ) {
186
- // there's a bug in HttpClient 4.0.x that on some occasions causes
187
- // DefaultRequestExecutor to throw an NPE, see
188
- // https://code.google.com/p/android/issues/detail?id=5255
189
- cause = new IOException ("NPE in HttpClient: " + e .getMessage ());
190
- retry = retryHandler .retryRequest (cause , ++executionCount , context );
191
- } catch (IOException e ) {
192
- if (isCancelled ()) {
193
- // Eating exception, as the request was cancelled
194
- return ;
195
- }
196
- cause = e ;
197
- retry = retryHandler .retryRequest (cause , ++executionCount , context );
198
- }
199
- if (retry ) {
200
- responseHandler .sendRetryMessage (executionCount );
201
- }
202
- }
203
- } catch (Exception e ) {
204
- // catch anything else to ensure failure message is propagated
205
- AsyncHttpClient .log .e ("AsyncHttpRequest" , "Unhandled exception origin cause" , e );
206
- cause = new IOException ("Unhandled exception: " + e .getMessage ());
207
- }
208
-
209
- // cleaned up to throw IOException
210
- throw (cause );
211
- }
212
-
213
169
public boolean isCancelled () {
214
170
boolean cancelled = isCancelled .get ();
215
171
if (cancelled ) {
0 commit comments