Code example for HttpEntityEnclosingRequestBase

Methods: setEntity, setHeaders

0
			AsyncHttpResponseHandler responseHandler)
	{ 
		HttpEntityEnclosingRequestBase request = addEntityToRequestBase(
				new HttpPost(url), entity);
		if (headers != null)
			request.setHeaders(headers);
		sendRequest(httpClient, httpContext, request, contentType,
				responseHandler, context);
	} 
 
	// 
	// HTTP PUT 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);
	} 
 
	/** 
	 * 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);
	} 
 
	/** 
	 * Perform a HTTP PUT request and track the Android Context which initiated 
	 * the request. And set one-time headers for 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. And set one-time headers for the request 
	 *  
	 * @param context 
	 *            the Android Context which initiated the request. 
	 * @param url 
	 *            the URL to send the request to. 
	 * @param headers 
	 *            set one-time headers 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 put(Context context, String url, Header[] headers,
			HttpEntity entity, String contentType,
			AsyncHttpResponseHandler responseHandler)
	{ 
		HttpEntityEnclosingRequestBase request = addEntityToRequestBase(
				new HttpPut(url), entity);
		if (headers != null)
			request.setHeaders(headers);
		sendRequest(httpClient, httpContext, request, contentType,
				responseHandler, context);
	} 
 
	// 
	// HTTP DELETE Requests 
	// 
 
	/** 
	 * 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 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 context 
	 *            the Android Context which initiated the request. 
	 * @param url 
	 *            the URL to send the request to. 
	 * @param headers 
	 *            set one-time headers for this request 
	 * @param responseHandler 
	 *            the response handler instance that should handle the response. 
	 */ 
	public void delete(Context context, String url, Header[] headers,
			AsyncHttpResponseHandler responseHandler)
	{ 
		final HttpDelete delete = new HttpDelete(url);
		if (headers != null)
			delete.setHeaders(headers);
		sendRequest(httpClient, httpContext, delete, null, responseHandler,
				context);
	} 
 
	// Private stuff 
	protected 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? 
		} 
 
	} 
 
	public static String getUrlWithQueryString(String url, RequestParams params)
	{ 
		if (params != null)
		{ 
			String paramString = params.getParamString();
			if (url.indexOf("?") == -1)
			{ 
				url += "?" + paramString;
			} else 
			{ 
				url += "&" + paramString;
			} 
		} 
 
		return url;
	} 
 
	private HttpEntity paramsToEntity(RequestParams params)
	{ 
		HttpEntity entity = null;
 
		if (params != null)
		{ 
			entity = params.getEntity();
		} 
 
		return entity;
	} 
 
	private HttpEntityEnclosingRequestBase addEntityToRequestBase(
			HttpEntityEnclosingRequestBase requestBase, HttpEntity entity)
	{ 
		if (entity != null)
		{ 
			requestBase.setEntity(entity);
		} 
 
		return requestBase;
	}