63
63
64
64
import java .io .IOException ;
65
65
import java .io .InputStream ;
66
- import java .lang .ref .WeakReference ;
67
66
import java .util .HashMap ;
67
+ import java .util .Iterator ;
68
68
import java .util .LinkedList ;
69
69
import java .util .List ;
70
70
import java .util .Map ;
71
71
import java .util .WeakHashMap ;
72
+ import java .util .concurrent .ExecutorService ;
72
73
import java .util .concurrent .Executors ;
73
- import java .util .concurrent .Future ;
74
74
import java .util .concurrent .ThreadPoolExecutor ;
75
75
import java .util .zip .GZIPInputStream ;
76
76
91
91
* </pre>
92
92
*/
93
93
public class AsyncHttpClient {
94
- private static final String VERSION = "1.4.4" ;
95
94
96
- private static final int DEFAULT_MAX_CONNECTIONS = 10 ;
97
- private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
98
- private static final int DEFAULT_MAX_RETRIES = 5 ;
99
- private static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500 ;
100
- private static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192 ;
101
- private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
102
- private static final String ENCODING_GZIP = "gzip" ;
103
- private static final String LOG_TAG = "AsyncHttpClient" ;
95
+ public static final String VERSION = "1.4.5" ;
96
+ public static final int DEFAULT_MAX_CONNECTIONS = 10 ;
97
+ public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
98
+ public static final int DEFAULT_MAX_RETRIES = 5 ;
99
+ public static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500 ;
100
+ public static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192 ;
101
+ public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
102
+ public static final String ENCODING_GZIP = "gzip" ;
103
+ public static final String LOG_TAG = "AsyncHttpClient" ;
104
104
105
105
private int maxConnections = DEFAULT_MAX_CONNECTIONS ;
106
106
private int timeout = DEFAULT_SOCKET_TIMEOUT ;
107
107
108
108
private final DefaultHttpClient httpClient ;
109
109
private final HttpContext httpContext ;
110
- private ThreadPoolExecutor threadPool ;
111
- private final Map <Context , List <WeakReference < Future <?>> >> requestMap ;
110
+ private ExecutorService threadPool ;
111
+ private final Map <Context , List <RequestHandle >> requestMap ;
112
112
private final Map <String , String > clientHeaderMap ;
113
113
private boolean isUrlEncodingEnabled = true ;
114
114
@@ -209,8 +209,8 @@ public AsyncHttpClient(SchemeRegistry schemeRegistry) {
209
209
210
210
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager (httpParams , schemeRegistry );
211
211
212
- threadPool = ( ThreadPoolExecutor ) Executors .newFixedThreadPool ( DEFAULT_MAX_CONNECTIONS );
213
- requestMap = new WeakHashMap <Context , List <WeakReference < Future <?>> >>();
212
+ threadPool = Executors .newCachedThreadPool ( );
213
+ requestMap = new WeakHashMap <Context , List <RequestHandle >>();
214
214
clientHeaderMap = new HashMap <String , String >();
215
215
216
216
httpContext = new SyncBasicHttpContext (new BasicHttpContext ());
@@ -249,6 +249,18 @@ public void process(HttpResponse response, HttpContext context) {
249
249
httpClient .setHttpRequestRetryHandler (new RetryHandler (DEFAULT_MAX_RETRIES , DEFAULT_RETRY_SLEEP_TIME_MILLIS ));
250
250
}
251
251
252
+ public static void allowRetryExceptionClass (Class <?> cls ) {
253
+ if (cls != null ) {
254
+ RetryHandler .addClassToWhitelist (cls );
255
+ }
256
+ }
257
+
258
+ public static void blockRetryExceptionClass (Class <?> cls ) {
259
+ if (cls != null ) {
260
+ RetryHandler .addClassToBlacklist (cls );
261
+ }
262
+ }
263
+
252
264
/**
253
265
* Get the underlying HttpClient instance. This is useful for setting additional fine-grained
254
266
* settings for requests by accessing the client's ConnectionManager, HttpParams and
@@ -475,21 +487,16 @@ public void clearBasicAuth() {
475
487
* pending requests.
476
488
*/
477
489
public void cancelRequests (Context context , boolean mayInterruptIfRunning ) {
478
- List <WeakReference < Future <?>> > requestList = requestMap .get (context );
490
+ List <RequestHandle > requestList = requestMap .get (context );
479
491
if (requestList != null ) {
480
- for (WeakReference <Future <?>> requestRef : requestList ) {
481
- Future <?> request = requestRef .get ();
482
- if (request != null ) {
483
- request .cancel (mayInterruptIfRunning );
484
- }
492
+ for (RequestHandle requestHandle : requestList ) {
493
+ requestHandle .cancel (mayInterruptIfRunning );
485
494
}
495
+ requestMap .remove (context );
486
496
}
487
- requestMap .remove (context );
488
497
}
489
498
490
- //
491
- // HTTP HEAD Requests
492
- //
499
+ // [+] HTTP HEAD
493
500
494
501
/**
495
502
* Perform a HTTP HEAD request, without any parameters.
@@ -558,10 +565,8 @@ public RequestHandle head(Context context, String url, Header[] headers, Request
558
565
context );
559
566
}
560
567
561
-
562
- //
563
- // HTTP GET Requests
564
- //
568
+ // [-] HTTP HEAD
569
+ // [+] HTTP GET
565
570
566
571
/**
567
572
* Perform a HTTP GET request, without any parameters.
@@ -630,10 +635,8 @@ public RequestHandle get(Context context, String url, Header[] headers, RequestP
630
635
context );
631
636
}
632
637
633
-
634
- //
635
- // HTTP POST Requests
636
- //
638
+ // [-] HTTP GET
639
+ // [+] HTTP POST
637
640
638
641
/**
639
642
* Perform a HTTP POST request, without any parameters.
@@ -732,9 +735,8 @@ public RequestHandle post(Context context, String url, Header[] headers, HttpEnt
732
735
return sendRequest (httpClient , httpContext , request , contentType , responseHandler , context );
733
736
}
734
737
735
- //
736
- // HTTP PUT Requests
737
- //
738
+ // [-] HTTP POST
739
+ // [+] HTTP PUT
738
740
739
741
/**
740
742
* Perform a HTTP PUT request, without any parameters.
@@ -811,9 +813,8 @@ public RequestHandle put(Context context, String url, Header[] headers, HttpEnti
811
813
return sendRequest (httpClient , httpContext , request , contentType , responseHandler , context );
812
814
}
813
815
814
- //
815
- // HTTP DELETE Requests
816
- //
816
+ // [-] HTTP PUT
817
+ // [+] HTTP DELETE
817
818
818
819
/**
819
820
* Perform a HTTP DELETE request.
@@ -870,6 +871,8 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
870
871
return sendRequest (httpClient , httpContext , httpDelete , null , responseHandler , context );
871
872
}
872
873
874
+ // [-] HTTP DELETE
875
+
873
876
/**
874
877
* Puts a new request in queue as a new thread in pool to be executed
875
878
*
@@ -884,28 +887,35 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
884
887
*/
885
888
protected RequestHandle sendRequest (DefaultHttpClient client , HttpContext httpContext , HttpUriRequest uriRequest , String contentType , ResponseHandlerInterface responseHandler , Context context ) {
886
889
if (contentType != null ) {
887
- uriRequest .addHeader ("Content-Type" , contentType );
890
+ uriRequest .setHeader ("Content-Type" , contentType );
888
891
}
889
892
890
893
responseHandler .setRequestHeaders (uriRequest .getAllHeaders ());
891
894
responseHandler .setRequestURI (uriRequest .getURI ());
892
895
893
- Future <?> request = threadPool .submit (new AsyncHttpRequest (client , httpContext , uriRequest , responseHandler ));
896
+ AsyncHttpRequest request = new AsyncHttpRequest (client , httpContext , uriRequest , responseHandler );
897
+ threadPool .submit (request );
898
+ RequestHandle requestHandle = new RequestHandle (request );
894
899
895
900
if (context != null ) {
896
901
// Add request to request map
897
- List <WeakReference < Future <?>> > requestList = requestMap .get (context );
902
+ List <RequestHandle > requestList = requestMap .get (context );
898
903
if (requestList == null ) {
899
- requestList = new LinkedList <WeakReference < Future <?>> >();
904
+ requestList = new LinkedList <RequestHandle >();
900
905
requestMap .put (context , requestList );
901
906
}
902
907
903
- requestList .add (new WeakReference < Future <?>>( request ) );
908
+ requestList .add (requestHandle );
904
909
905
- // TODO: Remove dead weakrefs from requestLists?
910
+ Iterator <RequestHandle > iterator = requestList .iterator ();
911
+ while (iterator .hasNext ()) {
912
+ if (iterator .next ().shouldBeGarbageCollected ()) {
913
+ iterator .remove ();
914
+ }
915
+ }
906
916
}
907
917
908
- return new RequestHandle ( request ) ;
918
+ return requestHandle ;
909
919
}
910
920
911
921
/**
0 commit comments