Code example for HttpEntityEnclosingRequestBase

Methods: setEntity, setHeaders

0
     * @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;
    }