Code example for BasicHttpProcessor

Methods: BasicHttpProcessor.addInterceptor, addInterceptor, addRequestInterceptor

0
		this.delegate = new DefaultHttpClient(ccm, params) {
			@Override 
			protected BasicHttpProcessor createHttpProcessor() {
				// Add interceptor to prevent making requests from main thread. 
				BasicHttpProcessor processor = super.createHttpProcessor();
				processor.addRequestInterceptor(sThreadCheckInterceptor);
				processor.addRequestInterceptor(new CurlLogger());
 
				return processor;
			} 
 
			@Override 
			protected HttpContext createHttpContext() {
				// Same as DefaultHttpClient.createHttpContext() minus the 
				// cookie store. 
				HttpContext context = new BasicHttpContext();
				context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY,
						getAuthSchemes());
				context.setAttribute(ClientContext.COOKIESPEC_REGISTRY,
						getCookieSpecs());
				context.setAttribute(ClientContext.CREDS_PROVIDER,
						getCredentialsProvider());
				return context;
			} 
		}; 
	} 
 
	@Override 
	protected void finalize() throws Throwable { 
		super.finalize(); 
		if (mLeakedException != null) {
			Log.e(TAG, "Leak found", mLeakedException);
			mLeakedException = null;
		} 
	} 
 
	/** 
	 * Modifies a request to indicate to the server that we would like a gzipped 
	 * response. (Uses the "Accept-Encoding" HTTP header.) 
	 *  
	 * @param request 
	 *            the request to modify 
	 * @see #getUngzippedContent 
	 */ 
	public static void modifyRequestToAcceptGzipResponse(HttpRequest request) {
		request.addHeader("Accept-Encoding", "gzip");
	} 
 
	/** 
	 * Gets the input stream from a response entity. If the entity is gzipped 
	 * then this will get a stream over the uncompressed data. 
	 *  
	 * @param entity 
	 *            the entity whose content should be read 
	 * @return the input stream to read from 
	 * @throws IOException 
	 */ 
	public static InputStream getUngzippedContent(HttpEntity entity)
			throws IOException { 
		InputStream responseStream = entity.getContent();
		if (responseStream == null)
			return responseStream;
		Header header = entity.getContentEncoding();
		if (header == null)
			return responseStream;
		String contentEncoding = header.getValue();
		if (contentEncoding == null)
			return responseStream;
		if (contentEncoding.contains("gzip"))
			responseStream = new GZIPInputStream(responseStream);
		return responseStream;
	} 
 
	/** 
	 * Release resources associated with this client. You must call this, or 
	 * significant resources (sockets and memory) may be leaked. 
	 */ 
	public void close() { 
		if (mLeakedException != null) {
			getConnectionManager().shutdown();
			mLeakedException = null;
		} 
	} 
 
	public HttpParams getParams() {
		return delegate.getParams();
	} 
 
	public ClientConnectionManager getConnectionManager() {
		return delegate.getConnectionManager();
	} 
 
	public HttpResponse execute(HttpUriRequest request) throws IOException {
		return delegate.execute(request);
	} 
 
	public HttpResponse execute(HttpUriRequest request, HttpContext context)
			throws IOException { 
		return delegate.execute(request, context);
	} 
 
	public HttpResponse execute(HttpHost target, HttpRequest request)
			throws IOException { 
		return delegate.execute(target, request);
	} 
 
	public HttpResponse execute(HttpHost target, HttpRequest request,
			HttpContext context) throws IOException {
		return delegate.execute(target, request, context);
	} 
 
	public <T> T execute(HttpUriRequest request,
			ResponseHandler<? extends T> responseHandler) throws IOException,
			ClientProtocolException { 
		return delegate.execute(request, responseHandler);
	} 
 
	public <T> T execute(HttpUriRequest request,
			ResponseHandler<? extends T> responseHandler, HttpContext context)
			throws IOException, ClientProtocolException { 
		return delegate.execute(request, responseHandler, context);
	} 
 
	public <T> T execute(HttpHost target, HttpRequest request,
			ResponseHandler<? extends T> responseHandler) throws IOException,
			ClientProtocolException { 
		return delegate.execute(target, request, responseHandler);
	}