65
65
66
66
import android .content .Context ;
67
67
68
+
69
+ /**
70
+ * The AsyncHttpClient can be used to make asynchronous GET, POST, PUT and
71
+ * DELETE HTTP requests in your Android applications. Requests can be made
72
+ * with additional parameters by passing a {@link RequestParams} instance,
73
+ * and responses can be handled by passing an anonymously overridden
74
+ * {@link AsyncHttpResponseHandler} instance.
75
+ * <p>
76
+ * For example:
77
+ * <p>
78
+ * <pre>
79
+ * AsyncHttpClient client = new AsyncHttpClient("My User Agent");
80
+ * client.get("http://www.google.com", new AsyncHttpResponseHandler() {
81
+ * @Override
82
+ * public void onSuccess(String response) {
83
+ * System.out.println(response);
84
+ * }
85
+ * });
86
+ * </pre>
87
+ */
68
88
public class AsyncHttpClient {
69
- public static final int DEFAULT_MAX_CONNECTIONS = 10 ;
70
- public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
71
- public static final int DEFAULT_MAX_RETRIES = 5 ;
89
+ private static final int DEFAULT_MAX_CONNECTIONS = 10 ;
90
+ private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
91
+ private static final int DEFAULT_MAX_RETRIES = 5 ;
72
92
private static final String ENCODING = "UTF-8" ;
73
93
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
74
94
private static final String ENCODING_GZIP = "gzip" ;
@@ -83,7 +103,7 @@ public class AsyncHttpClient {
83
103
84
104
/**
85
105
* Creates a new AsyncHttpClient which will identify itself with the user agent userAgent
86
- * @param userAgent The identifier to use in the User-Agent header in requests
106
+ * @param userAgent the identifier to use in the User-Agent header in requests
87
107
*/
88
108
public AsyncHttpClient (String userAgent ) {
89
109
BasicHttpParams httpParams = new BasicHttpParams ();
@@ -135,14 +155,35 @@ public void process(HttpResponse response, HttpContext context) {
135
155
requestMap = new WeakHashMap <Context , List <WeakReference <Future >>>();
136
156
}
137
157
158
+ /**
159
+ * Sets an optional CookieStore to use when making requests
160
+ * @param cookieStore The CookieStore implementation to use, usually an instance of {@link PersistentCookieStore}
161
+ */
138
162
public void setCookieStore (CookieStore cookieStore ) {
139
163
httpContext .setAttribute (ClientContext .COOKIE_STORE , cookieStore );
140
164
}
141
165
166
+ /**
167
+ * Overrides the threadpool implementation used when queuing/pooling
168
+ * requests. By default, Executors.newCachedThreadPool() is used.
169
+ * @param threadPool an instance of {@link ThreadPoolExecutor} to use for queuing/pooling requests.
170
+ */
142
171
public void setThreadPool (ThreadPoolExecutor threadPool ) {
143
172
this .threadPool = threadPool ;
144
173
}
145
174
175
+ /**
176
+ * Cancels any pending (or potentially active) requests associated with the
177
+ * passed Context.
178
+ * <p>
179
+ * <b>Note:</b> This will only affect requests which were created with a non-null
180
+ * android Context. This method is intended to be used in the onDestroy
181
+ * method of your android activities to destroy all requests which are no
182
+ * longer required.
183
+ *
184
+ * @param context the android Context instance associated to the request.
185
+ * @param mayInterruptIfRunning specifies if active requests should be cancelled along with pending requests.
186
+ */
146
187
public void cancelRequests (Context context , boolean mayInterruptIfRunning ) {
147
188
List <WeakReference <Future >> requestList = requestMap .get (context );
148
189
if (requestList != null ) {
@@ -157,80 +198,172 @@ public void cancelRequests(Context context, boolean mayInterruptIfRunning) {
157
198
}
158
199
159
200
160
- // Http GET Requests
201
+ //
202
+ // HTTP GET Requests
203
+ //
204
+
205
+ /**
206
+ * Perform a HTTP GET request, without any parameters.
207
+ * @param url the URL to send the request to.
208
+ * @param responseHandler the response handler instance that should handle the response.
209
+ */
161
210
public void get (String url , AsyncHttpResponseHandler responseHandler ) {
162
211
get (null , url , null , responseHandler );
163
212
}
164
213
214
+ /**
215
+ * Perform a HTTP GET request with parameters.
216
+ * @param url the URL to send the request to.
217
+ * @param params additional GET parameters to send with the request.
218
+ * @param responseHandler the response handler instance that should handle the response.
219
+ */
165
220
public void get (String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
166
221
get (null , url , params , responseHandler );
167
222
}
168
223
224
+ /**
225
+ * Perform a HTTP GET request without any parameters and track the Android Context which initiated the request.
226
+ * @param context the Android Context which initiated the request.
227
+ * @param url the URL to send the request to.
228
+ * @param responseHandler the response handler instance that should handle the response.
229
+ */
169
230
public void get (Context context , String url , AsyncHttpResponseHandler responseHandler ) {
170
231
get (context , url , null , responseHandler );
171
232
}
172
233
234
+ /**
235
+ * Perform a HTTP GET request and track the Android Context which initiated the request.
236
+ * @param context the Android Context which initiated the request.
237
+ * @param url the URL to send the request to.
238
+ * @param params additional GET parameters to send with the request.
239
+ * @param responseHandler the response handler instance that should handle the response.
240
+ */
173
241
public void get (Context context , String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
174
242
sendRequest (httpClient , httpContext , new HttpGet (getUrlWithQueryString (url , params )), null , responseHandler , context );
175
243
}
176
244
177
245
178
- // Http POST Requests
246
+ //
247
+ // HTTP POST Requests
248
+ //
249
+
250
+ /**
251
+ * Perform a HTTP POST request, without any parameters.
252
+ * @param url the URL to send the request to.
253
+ * @param responseHandler the response handler instance that should handle the response.
254
+ */
179
255
public void post (String url , AsyncHttpResponseHandler responseHandler ) {
180
256
post (null , url , null , responseHandler );
181
257
}
182
258
259
+ /**
260
+ * Perform a HTTP POST request with parameters.
261
+ * @param url the URL to send the request to.
262
+ * @param params additional POST parameters or files to send with the request.
263
+ * @param responseHandler the response handler instance that should handle the response.
264
+ */
183
265
public void post (String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
184
266
post (null , url , params , responseHandler );
185
267
}
186
268
187
- public void post (Context context , String url , AsyncHttpResponseHandler responseHandler ) {
188
- post (context , url , null , responseHandler );
189
- }
190
-
269
+ /**
270
+ * Perform a HTTP POST request and track the Android Context which initiated the request.
271
+ * @param context the Android Context which initiated the request.
272
+ * @param url the URL to send the request to.
273
+ * @param params additional POST parameters or files to send with the request.
274
+ * @param responseHandler the response handler instance that should handle the response.
275
+ */
191
276
public void post (Context context , String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
192
277
post (context , url , paramsToEntity (params ), null , responseHandler );
193
278
}
194
279
280
+ /**
281
+ * Perform a HTTP POST request and track the Android Context which initiated the request.
282
+ * @param context the Android Context which initiated the request.
283
+ * @param url the URL to send the request to.
284
+ * @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}.
285
+ * @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
286
+ * @param responseHandler the response handler instance that should handle the response.
287
+ */
195
288
public void post (Context context , String url , HttpEntity entity , String contentType , AsyncHttpResponseHandler responseHandler ) {
196
289
sendRequest (httpClient , httpContext , addEntityToRequestBase (new HttpPost (url ), entity ), contentType , responseHandler , context );
197
290
}
198
291
199
292
200
- // Http PUT Requests
293
+ //
294
+ // HTTP PUT Requests
295
+ //
296
+
297
+ /**
298
+ * Perform a HTTP PUT request, without any parameters.
299
+ * @param url the URL to send the request to.
300
+ * @param responseHandler the response handler instance that should handle the response.
301
+ */
201
302
public void put (String url , AsyncHttpResponseHandler responseHandler ) {
202
303
put (null , url , null , responseHandler );
203
304
}
204
305
306
+ /**
307
+ * Perform a HTTP PUT request with parameters.
308
+ * @param url the URL to send the request to.
309
+ * @param params additional PUT parameters or files to send with the request.
310
+ * @param responseHandler the response handler instance that should handle the response.
311
+ */
205
312
public void put (String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
206
313
put (null , params , responseHandler );
207
314
}
208
315
209
- public void put (Context context , String url , AsyncHttpResponseHandler responseHandler ) {
210
- put (context , url , null , responseHandler );
211
- }
212
-
316
+ /**
317
+ * Perform a HTTP PUT request and track the Android Context which initiated the request.
318
+ * @param context the Android Context which initiated the request.
319
+ * @param url the URL to send the request to.
320
+ * @param params additional PUT parameters or files to send with the request.
321
+ * @param responseHandler the response handler instance that should handle the response.
322
+ */
213
323
public void put (Context context , String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
214
324
put (context , url , paramsToEntity (params ), null , responseHandler );
215
325
}
216
326
327
+ /**
328
+ * Perform a HTTP PUT request and track the Android Context which initiated the request.
329
+ * @param context the Android Context which initiated the request.
330
+ * @param url the URL to send the request to.
331
+ * @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}.
332
+ * @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
333
+ * @param responseHandler the response handler instance that should handle the response.
334
+ */
217
335
public void put (Context context , String url , HttpEntity entity , String contentType , AsyncHttpResponseHandler responseHandler ) {
218
336
sendRequest (httpClient , httpContext , addEntityToRequestBase (new HttpPut (url ), entity ), contentType , responseHandler , context );
219
337
}
220
338
221
339
222
- // Http DELETE Requests
340
+ //
341
+ // HTTP DELETE Requests
342
+ //
343
+
344
+ /**
345
+ * Perform a HTTP DELETE request.
346
+ * @param url the URL to send the request to.
347
+ * @param responseHandler the response handler instance that should handle the response.
348
+ */
223
349
public void delete (String url , AsyncHttpResponseHandler responseHandler ) {
224
350
delete (null , url , responseHandler );
225
351
}
226
352
353
+ /**
354
+ * Perform a HTTP DELETE request.
355
+ * @param context the Android Context which initiated the request.
356
+ * @param url the URL to send the request to.
357
+ * @param responseHandler the response handler instance that should handle the response.
358
+ */
227
359
public void delete (Context context , String url , AsyncHttpResponseHandler responseHandler ) {
228
360
final HttpDelete delete = new HttpDelete (url );
229
361
sendRequest (httpClient , httpContext , delete , null , responseHandler , context );
230
362
}
231
363
232
364
233
- // Private stuf
365
+
366
+ // Private stuff
234
367
private void sendRequest (DefaultHttpClient client , HttpContext httpContext , HttpUriRequest uriRequest , String contentType , AsyncHttpResponseHandler responseHandler , Context context ) {
235
368
if (contentType != null ) {
236
369
uriRequest .addHeader ("Content-Type" , contentType );
0 commit comments