20
20
21
21
import java .io .IOException ;
22
22
import java .io .InputStream ;
23
- import java .util .concurrent .Executor ;
24
- import java .util .concurrent .ExecutorService ;
23
+ import java .util .List ;
24
+ import java .util .LinkedList ;
25
+ import java .util .Map ;
26
+ import java .util .WeakHashMap ;
25
27
import java .util .concurrent .Executors ;
28
+ import java .util .concurrent .Future ;
29
+ import java .util .concurrent .ThreadPoolExecutor ;
26
30
import java .util .zip .GZIPInputStream ;
27
31
28
32
import org .apache .http .Header ;
37
41
import org .apache .http .client .methods .HttpGet ;
38
42
import org .apache .http .client .methods .HttpPost ;
39
43
import org .apache .http .client .methods .HttpRequestBase ;
44
+ import org .apache .http .client .methods .HttpUriRequest ;
40
45
import org .apache .http .client .protocol .ClientContext ;
41
46
import org .apache .http .conn .params .ConnManagerParams ;
42
47
import org .apache .http .conn .params .ConnPerRouteBean ;
54
59
import org .apache .http .protocol .SyncBasicHttpContext ;
55
60
import org .apache .http .protocol .HttpContext ;
56
61
62
+ import android .content .Context ;
63
+ import android .util .Log ;
64
+
57
65
public class AsyncHttpClient {
58
66
public static final int DEFAULT_MAX_CONNECTIONS = 10 ;
59
- public static final int DEFAULT_SOCKET_TIMEOUT = 30 * 1000 ;
67
+ public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
60
68
public static final int DEFAULT_MAX_RETRIES = 5 ;
61
69
private static final String ENCODING = "UTF-8" ;
62
70
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
@@ -67,7 +75,8 @@ public class AsyncHttpClient {
67
75
68
76
private DefaultHttpClient httpClient ;
69
77
private HttpContext httpContext ;
70
- private Executor threadPool ;
78
+ private ThreadPoolExecutor threadPool ;
79
+ private Map <Context , List <Future >> requestMap ;
71
80
72
81
public AsyncHttpClient (String userAgent ) {
73
82
BasicHttpParams httpParams = new BasicHttpParams ();
@@ -114,37 +123,55 @@ public void process(HttpResponse response, HttpContext context) {
114
123
115
124
httpClient .setHttpRequestRetryHandler (new RetryHandler (DEFAULT_MAX_RETRIES ));
116
125
117
- threadPool = Executors .newCachedThreadPool ();
126
+ threadPool = (ThreadPoolExecutor )Executors .newCachedThreadPool ();
127
+
128
+ requestMap = new WeakHashMap <Context , List <Future >>();
118
129
}
119
130
120
131
public void setCookieStore (CookieStore cookieStore ) {
121
132
httpContext .setAttribute (ClientContext .COOKIE_STORE , cookieStore );
122
133
}
123
134
124
- public void setThreadPool (Executor threadPool ) {
135
+ public void setThreadPool (ThreadPoolExecutor threadPool ) {
125
136
this .threadPool = threadPool ;
126
137
}
127
138
128
139
public void get (String url , AsyncHttpResponseHandler responseHandler ) {
129
- get (url , null , responseHandler );
140
+ get (null , url , null , responseHandler );
141
+ }
142
+
143
+ public void get (Context context , String url , AsyncHttpResponseHandler responseHandler ) {
144
+ get (context , url , null , responseHandler );
130
145
}
131
146
132
147
public void get (String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
148
+ get (null , url , params , responseHandler );
149
+ }
150
+
151
+ public void get (Context context , String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
133
152
// Build and append query string (utf8 url encoded)
134
153
if (params != null ) {
135
154
String paramString = params .getParamString ();
136
155
url += "?" + paramString ;
137
156
}
138
157
139
158
// Fire up the request in a new thread
140
- threadPool . execute ( new AsyncHttpRequest ( httpClient , httpContext , new HttpGet (url ), responseHandler ) );
159
+ sendRequest ( httpClient , httpContext , new HttpGet (url ), responseHandler , context );
141
160
}
142
161
143
162
public void post (String url , AsyncHttpResponseHandler responseHandler ) {
144
- post (url , null , responseHandler );
163
+ post (null , url , null , responseHandler );
164
+ }
165
+
166
+ public void post (Context context , String url , AsyncHttpResponseHandler responseHandler ) {
167
+ post (context , url , null , responseHandler );
145
168
}
146
169
147
170
public void post (String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
171
+ post (null , url , params , responseHandler );
172
+ }
173
+
174
+ public void post (Context context , String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
148
175
// Build post object with params
149
176
final HttpPost post = new HttpPost (url );
150
177
if (params != null ) {
@@ -155,7 +182,31 @@ public void post(String url, RequestParams params, AsyncHttpResponseHandler resp
155
182
}
156
183
157
184
// Fire up the request in a new thread
158
- threadPool .execute (new AsyncHttpRequest (httpClient , httpContext , post , responseHandler ));
185
+ sendRequest (httpClient , httpContext , post , responseHandler , context );
186
+ }
187
+
188
+ public void cancelRequests (Context context , boolean mayInterruptIfRunning ) {
189
+ List <Future > requestList = requestMap .get (context );
190
+ if (requestList != null ) {
191
+ for (Future request : requestList ) {
192
+ request .cancel (mayInterruptIfRunning );
193
+ }
194
+ }
195
+ requestMap .remove (context );
196
+ }
197
+
198
+ private void sendRequest (DefaultHttpClient client , HttpContext httpContext , HttpUriRequest uriRequest , AsyncHttpResponseHandler responseHandler , Context context ) {
199
+ Future request = threadPool .submit (new AsyncHttpRequest (client , httpContext , uriRequest , responseHandler ));
200
+
201
+ if (context != null ) {
202
+ List <Future > requestList = requestMap .get (context );
203
+ if (requestList == null ) {
204
+ requestList = new LinkedList <Future >();
205
+ requestMap .put (context , requestList );
206
+ }
207
+
208
+ requestList .add (request );
209
+ }
159
210
}
160
211
161
212
private static class InflatingEntity extends HttpEntityWrapper {
0 commit comments