Code example for HttpEntityEnclosingRequestBase

Methods: setEntity, setHeaders

0
		return entity;
	} 
 
	private HttpEntityEnclosingRequestBase addEntityToRequestBase(HttpEntityEnclosingRequestBase requestBase, HttpEntity entity) {
		if (entity != null) {
			requestBase.setEntity(entity);
		} 
 
		return requestBase;
	} 
 
	/** 
	 * 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, HttpResponseHandler 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, HttpResponseHandler responseHandler) {
		get(null, url, params, 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, HttpResponseHandler responseHandler) {
		get(context, url, null, responseHandler);
	} 
 
	/** 
	 * 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, HttpResponseHandler responseHandler) {
		sendRequest(httpClient, httpContext, new HttpGet(getUrlWithQueryString(url, params)), null, responseHandler, context);
	} 
 
	/** 
	 * Perform a HTTP GET request and track the Android Context which initiated 
	 * the request with customized headers 
	 *  
	 * @param url 
	 *            the URL to send the request to. 
	 * @param headers 
	 *            set headers only for this request 
	 * @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, Header[] headers, RequestParams params, HttpResponseHandler responseHandler) {
		HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
		if (headers != null)
			request.setHeaders(headers);
		sendRequest(httpClient, httpContext, request, null, responseHandler, context);
	} 
 
	// 
	// HTTP POST 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, HttpResponseHandler 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, HttpResponseHandler responseHandler) {
		post(null, url, params, responseHandler);
	} 
 
	/** 
	 * 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, HttpResponseHandler responseHandler) {
		post(context, url, paramsToEntity(params), null, responseHandler);
	} 
 
	/** 
	 * 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, HttpResponseHandler 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. Set headers only for this request 
	 *  
	 * @param context 
	 *            the Android Context which initiated the request. 
	 * @param url 
	 *            the URL to send the request to. 
	 * @param headers 
	 *            set headers only for this request 
	 * @param params 
	 *            additional POST parameters to send with the request. 
	 * @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, Header[] headers, RequestParams params, String contentType, HttpResponseHandler responseHandler) {
		HttpEntityEnclosingRequestBase request = new HttpPost(url);
		if (params != null)
			request.setEntity(paramsToEntity(params));
		if (headers != null)
			request.setHeaders(headers);
		sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
	} 
 
	/** 
	 * Perform a HTTP POST request and track the Android Context which initiated 
	 * the request. Set headers only for this request 
	 *  
	 * @param context 
	 *            the Android Context which initiated the request. 
	 * @param url 
	 *            the URL to send the request to. 
	 * @param headers 
	 *            set headers only for this request 
	 * @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, Header[] headers, HttpEntity entity, String contentType, HttpResponseHandler responseHandler) {
		HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPost(url), entity);
		if (headers != null)
			request.setHeaders(headers);
		sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
	} 
 
	// Private stuff 
	protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, HttpResponseHandler responseHandler, Context context) {