This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AsyncHttpClient.java
481 lines (412 loc) · 19.7 KB
/
AsyncHttpClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
Android Asynchronous Http Client
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.
*/
package com.madrobot.net;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.SyncBasicHttpContext;
import android.content.Context;
/**
* The AsyncHttpClient can be used to make asynchronous GET, POST, PUT and
* DELETE HTTP requests in your Android applications. Requests can be made
* with additional parameters by passing a {@link RequestParams} instance,
* and responses can be handled by passing an anonymously overridden
* {@link AsyncHttpResponseHandler} instance.
* <p>
* For example:
* <p>
* <pre>
* AsyncHttpClient client = new AsyncHttpClient();
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
* @Override
* public void onSuccess(String response) {
* System.out.println(response);
* }
* });
* </pre>
* <b>Requests sent using AsyncHttpClient do NOT have to be in a separate thread. {@link AsyncHttpClient} maintains its own thread pool.</b>
* @see RequestParams
* @see AsyncHttpResponseHandler
*/
public class AsyncHttpClient {
private static class InflatingEntity extends HttpEntityWrapper {
public InflatingEntity(HttpEntity wrapped) {
super(wrapped);
}
@Override
public InputStream getContent() throws IOException {
return new GZIPInputStream(wrappedEntity.getContent());
}
@Override
public long getContentLength() {
return -1;
}
}
private static final int DEFAULT_MAX_CONNECTIONS = 10;
private static final int DEFAULT_MAX_RETRIES = 5;
private static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
private static final String ENCODING_GZIP = "gzip";
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
private static int maxConnections = DEFAULT_MAX_CONNECTIONS;
private static int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
private static final String VERSION = "1.3.1";
private final Map<String, String> clientHeaderMap;
private final DefaultHttpClient httpClient;
private final HttpContext httpContext;
private final Map<Context, List<WeakReference<Future<?>>>> requestMap;
private ThreadPoolExecutor threadPool;
/**
* Creates a new AsyncHttpClient.
*/
public AsyncHttpClient() {
BasicHttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams, socketTimeout);
ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
HttpConnectionParams.setTcpNoDelay(httpParams, true);
HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION));
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
httpContext = new SyncBasicHttpContext(new BasicHttpContext());
httpClient = new DefaultHttpClient(cm, httpParams);
httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) {
if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
for (String header : clientHeaderMap.keySet()) {
request.addHeader(header, clientHeaderMap.get(header));
}
}
});
httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context) {
final HttpEntity entity = response.getEntity();
final Header encoding = entity.getContentEncoding();
if (encoding != null) {
for (HeaderElement element : encoding.getElements()) {
if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
response.setEntity(new InflatingEntity(response.getEntity()));
break;
}
}
}
}
});
httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));
threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
clientHeaderMap = new HashMap<String, String>();
}
private HttpEntityEnclosingRequestBase addEntityToRequestBase(HttpEntityEnclosingRequestBase requestBase, HttpEntity entity) {
if(entity != null){
requestBase.setEntity(entity);
}
return requestBase;
}
/**
* Sets headers that will be added to all requests this client makes (before sending).
* @param header the name of the header
* @param value the contents of the header
*/
public void addHeader(String header, String value) {
clientHeaderMap.put(header, value);
}
/**
* Cancels any pending (or potentially active) requests associated with the
* passed Context.
* <p>
* <b>Note:</b> This will only affect requests which were created with a non-null
* android Context. This method is intended to be used in the onDestroy
* method of your android activities to destroy all requests which are no
* longer required.
*
* @param context the android Context instance associated to the request.
* @param mayInterruptIfRunning specifies if active requests should be cancelled along with pending requests.
*/
public void cancelRequests(Context context, boolean mayInterruptIfRunning) {
List<WeakReference<Future<?>>> requestList = requestMap.get(context);
if(requestList != null) {
for(WeakReference<Future<?>> requestRef : requestList) {
Future<?> request = requestRef.get();
if(request != null) {
request.cancel(mayInterruptIfRunning);
}
}
}
requestMap.remove(context);
}
/**
* Perform a HTTP DELETE request.
* @param context the Android Context which initiated the request.
* @param url the URL to send the request to.
* @param responseHandler the response handler instance that should handle the response.
*/
public void delete(Context context, String url, AsyncHttpResponseHandler responseHandler) {
final HttpDelete delete = new HttpDelete(url);
sendRequest(httpClient, httpContext, delete, null, responseHandler, context);
}
/**
* Perform a HTTP DELETE request.
* @param url the URL to send the request to.
* @param responseHandler the response handler instance that should handle the response.
*/
public void delete(String url, AsyncHttpResponseHandler responseHandler) {
delete(null, url, responseHandler);
}
/**
* Perform a HTTP GET request without any parameters and track the Android Context which initiated the request.
* @param context the Android Context which initiated the request.
* @param url the URL to send the request to.
* @param responseHandler the response handler instance that should handle the response.
*/
public void get(Context context, String url, AsyncHttpResponseHandler responseHandler) {
get(context, url, null, responseHandler);
}
//
// HTTP GET Requests
//
/**
* Perform a HTTP GET request and track the Android Context which initiated the request.
* @param context the Android Context which initiated the request.
* @param url the URL to send the request to.
* @param params additional GET parameters to send with the request.
* @param responseHandler the response handler instance that should handle the response.
*/
public void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
sendRequest(httpClient, httpContext, new HttpGet(getUrlWithQueryString(url, params)), null, responseHandler, context);
}
/**
* Perform a HTTP GET request, without any parameters.
* @param url the URL to send the request to.
* @param responseHandler the response handler instance that should handle the response.
*/
public void get(String url, AsyncHttpResponseHandler responseHandler) {
get(null, url, null, responseHandler);
}
/**
* Perform a HTTP GET request with parameters.
* @param url the URL to send the request to.
* @param params additional GET parameters to send with the request.
* @param responseHandler the response handler instance that should handle the response.
*/
public void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
get(null, url, params, responseHandler);
}
/**
* Get the underlying HttpClient instance. This is useful for setting
* additional fine-grained settings for requests by accessing the
* client's ConnectionManager, HttpParams and SchemeRegistry.
*/
public HttpClient getHttpClient() {
return this.httpClient;
}
//
// HTTP POST Requests
//
private String getUrlWithQueryString(String url, RequestParams params) {
if(params != null) {
String paramString = params.getParamString();
url += "?" + paramString;
}
return url;
}
private HttpEntity paramsToEntity(RequestParams params) {
HttpEntity entity = null;
if(params != null) {
entity = params.getEntity();
}
return entity;
}
/**
* Perform a HTTP POST request and track the Android Context which initiated the request.
* @param context the Android Context which initiated the request.
* @param url the URL to send the request to.
* @param entity a raw {@link HttpEntity} to send with the request, for example, use this to send string/json/xml payloads to a server by passing a {@link org.apache.http.entity.StringEntity}.
* @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
* @param responseHandler the response handler instance that should handle the response.
*/
public void post(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPost(url), entity), contentType, responseHandler, context);
}
/**
* Perform a HTTP POST request and track the Android Context which initiated the request.
* @param context the Android Context which initiated the request.
* @param url the URL to send the request to.
* @param params additional POST parameters or files to send with the request.
* @param responseHandler the response handler instance that should handle the response.
*/
public void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
post(context, url, paramsToEntity(params), null, responseHandler);
}
//
// HTTP PUT Requests
//
/**
* Perform a HTTP POST request, without any parameters.
* @param url the URL to send the request to.
* @param responseHandler the response handler instance that should handle the response.
*/
public void post(String url, AsyncHttpResponseHandler responseHandler) {
post(null, url, null, responseHandler);
}
/**
* Perform a HTTP POST request with parameters.
* @param url the URL to send the request to.
* @param params additional POST parameters or files to send with the request.
* @param responseHandler the response handler instance that should handle the response.
*/
public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
post(null, url, params, responseHandler);
}
/**
* Perform a HTTP PUT request and track the Android Context which initiated the request.
* @param context the Android Context which initiated the request.
* @param url the URL to send the request to.
* @param entity a raw {@link HttpEntity} to send with the request, for example, use this to send string/json/xml payloads to a server by passing a {@link org.apache.http.entity.StringEntity}.
* @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
* @param responseHandler the response handler instance that should handle the response.
*/
public void put(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPut(url), entity), contentType, responseHandler, context);
}
/**
* Perform a HTTP PUT request and track the Android Context which initiated the request.
* @param context the Android Context which initiated the request.
* @param url the URL to send the request to.
* @param params additional PUT parameters or files to send with the request.
* @param responseHandler the response handler instance that should handle the response.
*/
public void put(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
put(context, url, paramsToEntity(params), null, responseHandler);
}
//
// HTTP DELETE Requests
//
/**
* Perform a HTTP PUT request, without any parameters.
* @param url the URL to send the request to.
* @param responseHandler the response handler instance that should handle the response.
*/
public void put(String url, AsyncHttpResponseHandler responseHandler) {
put(null, url, null, responseHandler);
}
/**
* Perform a HTTP PUT request with parameters.
* @param url the URL to send the request to.
* @param params additional PUT parameters or files to send with the request.
* @param responseHandler the response handler instance that should handle the response.
*/
public void put(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
put(null, url, params, responseHandler);
}
// Private stuff
private void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
if(contentType != null) {
uriRequest.addHeader("Content-Type", contentType);
}
Future<?> request = threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));
if(context != null) {
// Add request to request map
List<WeakReference<Future<?>>> requestList = requestMap.get(context);
if(requestList == null) {
requestList = new LinkedList<WeakReference<Future<?>>>();
requestMap.put(context, requestList);
}
requestList.add(new WeakReference<Future<?>>(request));
// TODO: Remove dead weakrefs from requestLists?
}
}
/**
* Sets an optional CookieStore to use when making requests
* @param cookieStore The CookieStore implementation to use, usually an instance of {@link PersistentCookieStore}
*/
public void setCookieStore(CookieStore cookieStore) {
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
/**
* Sets the SSLSocketFactory to user when making requests. By default,
* a new, default SSLSocketFactory is used.
* @param sslSocketFactory the socket factory to use for https requests.
*/
public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
this.httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, 443));
}
/**
* Overrides the threadpool implementation used when queuing/pooling
* requests. By default, Executors.newCachedThreadPool() is used.
* @param threadPool an instance of {@link ThreadPoolExecutor} to use for queuing/pooling requests.
*/
public void setThreadPool(ThreadPoolExecutor threadPool) {
this.threadPool = threadPool;
}
/**
* Sets the User-Agent header to be sent with each request. By default,
* "Android Asynchronous Http Client/VERSION (http://loopj.com/android-async-http/)" is used.
* @param userAgent the string to use in the User-Agent header.
*/
public void setUserAgent(String userAgent) {
HttpProtocolParams.setUserAgent(this.httpClient.getParams(), userAgent);
}
}