58
58
import org .apache .http .impl .client .DefaultHttpClient ;
59
59
import org .apache .http .impl .conn .tsccm .ThreadSafeClientConnManager ;
60
60
import org .apache .http .params .BasicHttpParams ;
61
+ import org .apache .http .params .HttpParams ;
61
62
import org .apache .http .params .HttpConnectionParams ;
62
63
import org .apache .http .params .HttpProtocolParams ;
63
64
import org .apache .http .protocol .BasicHttpContext ;
@@ -200,6 +201,17 @@ public void setUserAgent(String userAgent) {
200
201
HttpProtocolParams .setUserAgent (this .httpClient .getParams (), userAgent );
201
202
}
202
203
204
+ /**
205
+ * Sets the connection time oout. By default, 10 seconds
206
+ * @param timeout the connect/socket timeout in milliseconds
207
+ */
208
+ public void setTimeout (int timeout ){
209
+ final HttpParams httpParams = this .httpClient .getParams ();
210
+ ConnManagerParams .setTimeout (httpParams , timeout );
211
+ HttpConnectionParams .setSoTimeout (httpParams , timeout );
212
+ HttpConnectionParams .setConnectionTimeout (httpParams , timeout );
213
+ }
214
+
203
215
/**
204
216
* Sets the SSLSocketFactory to user when making requests. By default,
205
217
* a new, default SSLSocketFactory is used.
@@ -287,6 +299,23 @@ public void get(Context context, String url, AsyncHttpResponseHandler responseHa
287
299
public void get (Context context , String url , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
288
300
sendRequest (httpClient , httpContext , new HttpGet (getUrlWithQueryString (url , params )), null , responseHandler , context );
289
301
}
302
+
303
+ /**
304
+ * Perform a HTTP GET request and track the Android Context which initiated
305
+ * the request with customized headers
306
+ *
307
+ * @param url the URL to send the request to.
308
+ * @param headers set headers only for this request
309
+ * @param params additional GET parameters to send with the request.
310
+ * @param responseHandler the response handler instance that should handle
311
+ * the response.
312
+ */
313
+ public void get (Context context , String url , Header [] headers , RequestParams params , AsyncHttpResponseHandler responseHandler ) {
314
+ HttpUriRequest request = new HttpGet (getUrlWithQueryString (url , params ));
315
+ if (headers != null ) request .setHeaders (headers );
316
+ sendRequest (httpClient , httpContext , request , null , responseHandler ,
317
+ context );
318
+ }
290
319
291
320
292
321
//
@@ -335,6 +364,29 @@ public void post(Context context, String url, HttpEntity entity, String contentT
335
364
sendRequest (httpClient , httpContext , addEntityToRequestBase (new HttpPost (url ), entity ), contentType , responseHandler , context );
336
365
}
337
366
367
+ /**
368
+ * Perform a HTTP POST request and track the Android Context which initiated
369
+ * the request. Set headers only for this request
370
+ *
371
+ * @param context the Android Context which initiated the request.
372
+ * @param url the URL to send the request to.
373
+ * @param headers set headers only for this request
374
+ * @param entity a raw {@link HttpEntity} to send with the request, for
375
+ * example, use this to send string/json/xml payloads to a server by
376
+ * passing a {@link org.apache.http.entity.StringEntity}.
377
+ * @param contentType the content type of the payload you are sending, for
378
+ * example application/json if sending a json payload.
379
+ * @param responseHandler the response handler instance that should handle
380
+ * the response.
381
+ */
382
+ public void post (Context context , String url , Header [] headers , RequestParams params , String contentType ,
383
+ AsyncHttpResponseHandler responseHandler ) {
384
+ HttpEntityEnclosingRequestBase request = new HttpPost (url );
385
+ if (params != null ) request .setEntity (paramsToEntity (params ));
386
+ if (headers != null ) request .setHeaders (headers );
387
+ sendRequest (httpClient , httpContext , request , contentType ,
388
+ responseHandler , context );
389
+ }
338
390
339
391
//
340
392
// HTTP PUT Requests
@@ -372,6 +424,7 @@ public void put(Context context, String url, RequestParams params, AsyncHttpResp
372
424
373
425
/**
374
426
* Perform a HTTP PUT request and track the Android Context which initiated the request.
427
+ * And set one-time headers for the request
375
428
* @param context the Android Context which initiated the request.
376
429
* @param url the URL to send the request to.
377
430
* @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}.
@@ -381,7 +434,22 @@ public void put(Context context, String url, RequestParams params, AsyncHttpResp
381
434
public void put (Context context , String url , HttpEntity entity , String contentType , AsyncHttpResponseHandler responseHandler ) {
382
435
sendRequest (httpClient , httpContext , addEntityToRequestBase (new HttpPut (url ), entity ), contentType , responseHandler , context );
383
436
}
384
-
437
+
438
+ /**
439
+ * Perform a HTTP PUT request and track the Android Context which initiated the request.
440
+ * And set one-time headers for the request
441
+ * @param context the Android Context which initiated the request.
442
+ * @param url the URL to send the request to.
443
+ * @param headers set one-time headers for this request
444
+ * @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}.
445
+ * @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
446
+ * @param responseHandler the response handler instance that should handle the response.
447
+ */
448
+ public void put (Context context , String url ,Header [] headers , HttpEntity entity , String contentType , AsyncHttpResponseHandler responseHandler ) {
449
+ HttpEntityEnclosingRequestBase request = addEntityToRequestBase (new HttpPut (url ), entity );
450
+ if (headers != null ) request .setHeaders (headers );
451
+ sendRequest (httpClient , httpContext , request , contentType , responseHandler , context );
452
+ }
385
453
386
454
//
387
455
// HTTP DELETE Requests
@@ -406,7 +474,19 @@ public void delete(Context context, String url, AsyncHttpResponseHandler respons
406
474
final HttpDelete delete = new HttpDelete (url );
407
475
sendRequest (httpClient , httpContext , delete , null , responseHandler , context );
408
476
}
409
-
477
+
478
+ /**
479
+ * Perform a HTTP DELETE request.
480
+ * @param context the Android Context which initiated the request.
481
+ * @param url the URL to send the request to.
482
+ * @param headers set one-time headers for this request
483
+ * @param responseHandler the response handler instance that should handle the response.
484
+ */
485
+ public void delete (Context context , String url , Header [] headers , AsyncHttpResponseHandler responseHandler ) {
486
+ final HttpDelete delete = new HttpDelete (url );
487
+ if (headers != null ) delete .setHeaders (headers );
488
+ sendRequest (httpClient , httpContext , delete , null , responseHandler , context );
489
+ }
410
490
411
491
412
492
// Private stuff
0 commit comments