Skip to content

Commit 00b523f

Browse files
author
yongsheng
committed
Add functions performing request with customized one-time headers
1 parent e898750 commit 00b523f

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

src/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,23 @@ public void get(Context context, String url, AsyncHttpResponseHandler responseHa
287287
public void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
288288
sendRequest(httpClient, httpContext, new HttpGet(getUrlWithQueryString(url, params)), null, responseHandler, context);
289289
}
290+
291+
/**
292+
* Perform a HTTP GET request and track the Android Context which initiated
293+
* the request with customized headers
294+
*
295+
* @param url the URL to send the request to.
296+
* @param headers set headers only for this request
297+
* @param params additional GET parameters to send with the request.
298+
* @param responseHandler the response handler instance that should handle
299+
* the response.
300+
*/
301+
public void get(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler) {
302+
HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
303+
if(headers != null) request.setHeaders(headers);
304+
sendRequest(httpClient, httpContext, request, null, responseHandler,
305+
context);
306+
}
290307

291308

292309
//
@@ -335,6 +352,29 @@ public void post(Context context, String url, HttpEntity entity, String contentT
335352
sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPost(url), entity), contentType, responseHandler, context);
336353
}
337354

355+
/**
356+
* Perform a HTTP POST request and track the Android Context which initiated
357+
* the request. Set headers only for this request
358+
*
359+
* @param context the Android Context which initiated the request.
360+
* @param url the URL to send the request to.
361+
* @param headers set headers only for this request
362+
* @param entity a raw {@link HttpEntity} to send with the request, for
363+
* example, use this to send string/json/xml payloads to a server by
364+
* passing a {@link org.apache.http.entity.StringEntity}.
365+
* @param contentType the content type of the payload you are sending, for
366+
* example application/json if sending a json payload.
367+
* @param responseHandler the response handler instance that should handle
368+
* the response.
369+
*/
370+
public void post(Context context, String url, Header[] headers, RequestParams params, String contentType,
371+
AsyncHttpResponseHandler responseHandler) {
372+
HttpEntityEnclosingRequestBase request = new HttpPost(url);
373+
if(params != null) request.setEntity(paramsToEntity(params));
374+
if(headers != null) request.setHeaders(headers);
375+
sendRequest(httpClient, httpContext, request, contentType,
376+
responseHandler, context);
377+
}
338378

339379
//
340380
// HTTP PUT Requests
@@ -372,6 +412,7 @@ public void put(Context context, String url, RequestParams params, AsyncHttpResp
372412

373413
/**
374414
* Perform a HTTP PUT request and track the Android Context which initiated the request.
415+
* And set one-time headers for the request
375416
* @param context the Android Context which initiated the request.
376417
* @param url the URL to send the request to.
377418
* @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 +422,22 @@ public void put(Context context, String url, RequestParams params, AsyncHttpResp
381422
public void put(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
382423
sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPut(url), entity), contentType, responseHandler, context);
383424
}
384-
425+
426+
/**
427+
* Perform a HTTP PUT request and track the Android Context which initiated the request.
428+
* And set one-time headers for the request
429+
* @param context the Android Context which initiated the request.
430+
* @param url the URL to send the request to.
431+
* @param headers set one-time headers for this request
432+
* @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}.
433+
* @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
434+
* @param responseHandler the response handler instance that should handle the response.
435+
*/
436+
public void put(Context context, String url,Header[] headers, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
437+
HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPut(url), entity);
438+
if(headers != null) request.setHeaders(headers);
439+
sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
440+
}
385441

386442
//
387443
// HTTP DELETE Requests
@@ -406,7 +462,19 @@ public void delete(Context context, String url, AsyncHttpResponseHandler respons
406462
final HttpDelete delete = new HttpDelete(url);
407463
sendRequest(httpClient, httpContext, delete, null, responseHandler, context);
408464
}
409-
465+
466+
/**
467+
* Perform a HTTP DELETE request.
468+
* @param context the Android Context which initiated the request.
469+
* @param url the URL to send the request to.
470+
* @param headers set one-time headers for this request
471+
* @param responseHandler the response handler instance that should handle the response.
472+
*/
473+
public void delete(Context context, String url, Header[] headers, AsyncHttpResponseHandler responseHandler) {
474+
final HttpDelete delete = new HttpDelete(url);
475+
if(headers != null) delete.setHeaders(headers);
476+
sendRequest(httpClient, httpContext, delete, null, responseHandler, context);
477+
}
410478

411479

412480
// Private stuff

0 commit comments

Comments
 (0)