Example usage for org.apache.http.impl.client DefaultHttpClient addResponseInterceptor

List of usage examples for org.apache.http.impl.client DefaultHttpClient addResponseInterceptor

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient addResponseInterceptor.

Prototype

public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp, final int index) 

Source Link

Usage

From source file:com.riksof.a320.http.CoreHttpClient.java

/**
 * This method is used to execute Get Http Request
 * /* www. j  a va2s  .  c o  m*/
 * @param url
 * @return
 * @throws ServerException
 */
public String executeGet(String url) throws ServerException {

    // Response String
    String responseString = null;

    headers = new HashMap<String, String>();

    try {

        CacheConfig cacheConfig = new CacheConfig();
        cacheConfig.setMaxCacheEntries(1000);
        cacheConfig.setMaxObjectSizeBytes(1024 * 1024);

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, socketTimeOut);

        // Create http client object
        DefaultHttpClient realClient = new DefaultHttpClient(httpParameters);
        realClient.addResponseInterceptor(MakeCacheable.INSTANCE, 0);
        CachingHttpClient httpclient = new CachingHttpClient(realClient, cacheConfig);

        // Create http context object
        HttpContext localContext = new BasicHttpContext();

        // Check for cached data against URL
        if (Cache.getInstance().get(url) == null) {

            // Execute HTTP Get Request
            HttpGet httpget = new HttpGet(url);

            // Get http response
            HttpResponse response = httpclient.execute(httpget, localContext);

            for (Header h : response.getAllHeaders()) {
                headers.put(h.getName(), h.getValue());
            }

            // Create http entity object
            HttpEntity entity = response.getEntity();

            // Create and convert stream into string
            InputStream inStream = entity.getContent();
            responseString = convertStreamToString(inStream);

            // Cache url data
            Cache.getInstance().put(url, responseString);

        } else {

            // Returned cached data
            responseString = Cache.getInstance().get(url);
        }

    } catch (ClientProtocolException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("ClientProtocolException");
    } catch (ConnectTimeoutException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("ConnectTimeoutException");
    } catch (IOException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("Request Timeout");
    } catch (Exception e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("Exception");
    } finally {
    }
    return responseString;
}