Example usage for org.apache.http.protocol SyncBasicHttpContext SyncBasicHttpContext

List of usage examples for org.apache.http.protocol SyncBasicHttpContext SyncBasicHttpContext

Introduction

In this page you can find the example usage for org.apache.http.protocol SyncBasicHttpContext SyncBasicHttpContext.

Prototype

public SyncBasicHttpContext(HttpContext httpContext) 

Source Link

Usage

From source file:com.mike.aframe.http.KJHttp.java

/**
 * ?httpClient/*from w w w . j av  a2  s. co m*/
 */
private void initHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, config.getConnectTimeOut());
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(config.getMaxConnections()));
    ConnManagerParams.setMaxTotalConnections(httpParams, config.getMaxConnections());

    HttpConnectionParams.setSoTimeout(httpParams, config.getConnectTimeOut());
    HttpConnectionParams.setConnectionTimeout(httpParams, config.getConnectTimeOut());
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, config.getSocketBuffer());

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams, "KJLibrary");

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override
        public void process(HttpRequest request, HttpContext context) {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }

            for (Entry<String, String> entry : config.getHeader().entrySet()) {
                request.addHeader(entry.getKey(), entry.getValue());
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            if (entity == null) {
                return;
            }
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });
    threadPool = (ThreadPoolExecutor) KJThreadExecutors.newCachedThreadPool();
    httpClient.setHttpRequestRetryHandler(new RetryHandler(config.getReadTimeout()));
    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * 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.
 * @return RequestHandle of future request process
 *///from ww w.  j a  va 2s.  c  o  m
public RequestHandle get(Context context, String url, RequestParams params,
        ResponseHandlerInterface responseHandler) {
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext,
            new HttpGet(getUrlWithQueryString(isUrlEncodingEnabled, url, params)), null, responseHandler,
            context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * Perform a HTTP GET request and track the Android Context which initiated the request with
 * customized headers//from w ww .  j  av a 2 s .c  om
 *
 * @param context         Context to execute request against
 * @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.
 * @return RequestHandle of future request process
 */
public RequestHandle get(Context context, String url, Header[] headers, RequestParams params,
        ResponseHandlerInterface responseHandler) {
    HttpUriRequest request = new HttpGet(getUrlWithQueryString(isUrlEncodingEnabled, url, params));
    if (headers != null)
        request.setHeaders(headers);
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, request, null, responseHandler, context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * 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 org.apache.http.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 ha   ndler instance that should handle the response.
 * @return RequestHandle of future request process
 *//*  w ww . jav  a  2s.co  m*/
public RequestHandle post(Context context, String url, HttpEntity entity, String contentType,
        ResponseHandlerInterface responseHandler) {
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPost(url), entity), contentType,
            responseHandler, context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * Perform a HTTP POST request and track the Android Context which initiated the request. Set
 * headers only for this request/*from   w  w w  .  j  av  a2  s  . c o m*/
 *
 * @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.
 * @return RequestHandle of future request process
 */
public RequestHandle post(Context context, String url, Header[] headers, RequestParams params,
        String contentType, ResponseHandlerInterface responseHandler) {
    HttpEntityEnclosingRequestBase request = new HttpPost(url);
    if (params != null)
        request.setEntity(paramsToEntity(params, responseHandler));
    if (headers != null)
        request.setHeaders(headers);
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * Perform a HTTP POST request and track the Android Context which initiated the request. Set
 * headers only for this request//from   w w w .ja v  a2s  . co m
 *
 * @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.
 * @return RequestHandle of future request process
 */
public RequestHandle post(Context context, String url, Header[] headers, HttpEntity entity, String contentType,
        ResponseHandlerInterface responseHandler) {
    HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPost(url), entity);
    if (headers != null)
        request.setHeaders(headers);
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * Perform a HTTP PUT request and track the Android Context which initiated the request. And set
 * one-time headers for the request// w  ww.  j a v  a 2 s.  c  om
 *
 * @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.
 * @return RequestHandle of future request process
 */
public RequestHandle put(Context context, String url, HttpEntity entity, String contentType,
        ResponseHandlerInterface responseHandler) {
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPut(url), entity), contentType,
            responseHandler, context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * Perform a HTTP PUT request and track the Android Context which initiated the request. And set
 * one-time headers for the request//ww w  .j  a  v  a2 s  .c  o m
 *
 * @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.
 * @return RequestHandle of future request process
 */
public RequestHandle put(Context context, String url, Header[] headers, HttpEntity entity, String contentType,
        ResponseHandlerInterface responseHandler) {
    HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPut(url), entity);
    if (headers != null)
        request.setHeaders(headers);
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * Perform a HTTP DELETE request./*from   w  ww .j av  a 2 s .  c o  m*/
 *
 * @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.
 * @return RequestHandle of future request process
 */
public RequestHandle delete(Context context, String url, ResponseHandlerInterface responseHandler) {
    final HttpDelete delete = new HttpDelete(url);
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, delete, null, responseHandler, context);
}

From source file:com.enjoy.nerd.http.AsyncHttpClient.java

/**
 * Perform a HTTP DELETE request.//from   w w w.j  a va 2  s  .  c o m
 *
 * @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.
 * @return RequestHandle of future request process
 */
public RequestHandle delete(Context context, String url, Header[] headers,
        ResponseHandlerInterface responseHandler) {
    final HttpDelete delete = new HttpDelete(url);
    if (headers != null)
        delete.setHeaders(headers);
    SyncBasicHttpContext httpContext = new SyncBasicHttpContext(mBasicHttpContext);
    return sendRequest(httpClient, httpContext, delete, null, responseHandler, context);
}