Code example for HttpEntity

Methods: consumeContent, getContentLength, getContentType

0
        return httpResponse.getStatusLine().getStatusCode();
    } 
     
    public static String getContentType(HttpEntity entity)
    { 
        Header header = entity.getContentType();
        return header != null ? header.getElements()[0].getName() : null;
    } 
     
    public static long getContentLength(HttpEntity entity)
    { 
        return entity.getContentLength();
    } 
     
    // -------------------------------------------------------------------------------------------- 
 
    /** 
     * Creates the RestClient singleton instance if necessary. 
     * 
     * @param host 
     * @param httpPort 
     */ 
    private static RestClient instance(String host, int httpPort)
    { 
        synchronized(sSync)
        { 
            if (sImpl == null)
            { 
                sImpl = new RestClient(host, httpPort);
            } 
        } 
 
        return sImpl;
    } 
 
    /** 
     * Constructor 
     * 
     * @param host      Target host.  
     * @param httpPort  HTTP port to connect to the target host. 
     */ 
    private RestClient(String host, int httpPort)
    { 
        mHttpHost = new HttpHost(host, httpPort, "http");
 
        HttpParams hparams = new BasicHttpParams();
        hparams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
 
        List<Header> headers = new ArrayList<Header>();
        headers.add(new BasicHeader("Host", mHttpHost.toHostString()));
        hparams.setParameter(ClientPNames.DEFAULT_HEADERS, headers);
 
        mHttpClient = new DefaultHttpClient(hparams);
        mHttpClient.addRequestInterceptor(new HttpRequestInterceptor() {
            @Override 
            public void process(HttpRequest request, HttpContext context)
                        throws HttpException, IOException 
            { 
                synchronized (sSync)
                { 
                    if (mCredentials != null)
                    { 
                        request.addHeader(mAuthScheme.authenticate(mCredentials, request));
                    } 
                } 
            } 
        }); 
    } 
 
    /** 
     * Builds the URI addressing the resource in the REST server indicated by path. 
     * If the params parameter is not null or empty, the URI will contain query parameters. 
     * 
     * @param path      Path to the resource. 
     * @param queryArgs A bundle with query parameters. May be null or empty. 
     * 
     * @return java.net.URI instance, null on error. 
     */ 
    private URI makeURI(String path, QueryParams params)
    { 
        try 
        { 
            return URIUtils.createURI(mHttpHost.getSchemeName(),
                                      mHttpHost.getHostName(),
                                      mHttpHost.getPort(),
                                      path,
                                      params != null ? params.encode(mHttpClient) : null,
                                      null); 
        } 
        catch (URISyntaxException e)
        { 
//            android.util.Log.e(TAG, "URI syntaxError : "+e); 
            return null; 
        } 
    } 
 
    /** 
     * Executes the given HTTP request. If the server responds with an error >= 500, the entity 
     * body is assumed to contain an error message as HTML. 
     * 
     * @param httpRequest HTTP request to execute 
     * 
     * @return HTTP status code, 0 on exception. 
     *  
     * @throws IOException on network errors or the connection is aborted. 
     * @throws ClientProtocolException if there is a HTTP protocol error. 
     * @throws RestClient.Http5xxException if there is an internal server error (5xx) 
     * @throws RestClient.Http4xxException if there is a client error (4xx). 
     */ 
    private HttpResponse execute(HttpUriRequest httpRequest) throws IOException, 
                                                                    Http4xxException, 
                                                                    Http5xxException 
    { 
        HttpResponse r = mHttpClient.execute(httpRequest);
         
        if (r != null)
        { 
            int  c = getStatusCode(r);
         
            if (c >= 500)
            { 
                throw new Http5xxException(c, getRawData(r.getEntity()));
            } 
         
            if (c >= 400)
            { 
                throw new Http4xxException(c);
            } 
        } 
         
        return r;
    } 
 
    /** 
     * Returns an object holding the entity data if it is of the requested type. 
     * 
     * @param entity        HttpEntity instance. 
     * @param EntityParser A content parser of the expected type. 
     * 
     * @return An object holding the data of the expected type, if any. null if there is no data. 
     * 
     * @throws IOException and others depending on the {@link EntityParser} subclass.  
     * @throws org.apache.http.ParseException if the entity MIME type does not match the  
     *         parser's MIME type 
     */ 
    private Object getData(HttpEntity entity, EntityParser entityParser) throws IOException,
                                                                                ParseException 
    {    
        if (entity == null)
        { 
            return null; 
        } 
 
        // When a successfull request has no data, the Content-Length header is 0 and there is no  
        // Content-Type header. 
        if (getContentLength(entity) == 0)
        { 
            entity.consumeContent();
            return null; 
        } 
 
        String contentType = getContentType(entity);
        if (contentType == null || !entityParser.isForType(contentType))
        { 
            entity.consumeContent();
            throw new ParseException("Unexpected MIME type : "+contentType);
        } 
 
        return entityParser.parse(entity);
    } 
 
    // -------------------------------------------------------------------------------------------- 
 
    /**