Example usage for org.apache.http.entity BufferedHttpEntity getContent

List of usage examples for org.apache.http.entity BufferedHttpEntity getContent

Introduction

In this page you can find the example usage for org.apache.http.entity BufferedHttpEntity getContent.

Prototype

public InputStream getContent() throws IOException 

Source Link

Usage

From source file:Main.java

public static String getStringFromUrl(List<NameValuePair> nameValuePairs)
        throws ClientProtocolException, IOException {
    String url = "http://www.fsurugby.org/serve/request.php";
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = client.execute(httppost);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity buffer = new BufferedHttpEntity(entity);
    InputStream is = buffer.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line;/* w ww  .  ja  v  a 2  s  .  c  o m*/
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    return sb.toString();
}

From source file:com.huguesjohnson.retroleague.util.HttpFetch.java

public static InputStream fetch(String address, int timeout) throws MalformedURLException, IOException {
    HttpGet httpRequest = new HttpGet(URI.create(address));
    HttpClient httpclient = new DefaultHttpClient();
    final HttpParams httpParameters = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeout);
    HttpConnectionParams.setSoTimeout(httpParameters, timeout);
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    InputStream instream = bufHttpEntity.getContent();
    return (instream);
}

From source file:org.apache.abdera2.protocol.client.AbderaResponseHandler.java

private static InputStream getInputStream(HttpResponse response) throws IOException {
    InputStream in = null;//from ww  w.  j a va2  s .c o  m
    Header he = response.getFirstHeader("Content-Encoding");
    String ce = he != null ? he.getValue() : null;
    BufferedHttpEntity buffer = new BufferedHttpEntity(response.getEntity());
    in = buffer.getContent();
    if (ce != null && in != null)
        in = Compression.wrap(in, ce);
    return in;
}

From source file:com.onemorecastle.util.HttpFetch.java

public static Bitmap fetchBitmap(String imageAddress, int sampleSize)
        throws MalformedURLException, IOException {
    Bitmap bitmap = null;// w ww  . j  av a2  s.c o  m
    DefaultHttpClient httpclient = null;
    try {
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 2000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);
        HttpConnectionParams.setSocketBufferSize(httpParameters, 512);
        HttpGet httpRequest = new HttpGet(URI.create(imageAddress));
        httpclient = new DefaultHttpClient();
        httpclient.setParams(httpParameters);
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
        InputStream instream = bufHttpEntity.getContent();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //first decode with inJustDecodeBounds=true to check dimensions
        options.inJustDecodeBounds = true;
        options.inSampleSize = sampleSize;
        BitmapFactory.decodeStream(instream, null, options);
        //decode bitmap with inSampleSize set
        options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT);
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inDither = true;
        options.inJustDecodeBounds = false;
        //response=(HttpResponse)httpclient.execute(httpRequest);
        //entity=response.getEntity();
        //bufHttpEntity=new BufferedHttpEntity(entity);
        instream = bufHttpEntity.getContent();
        bitmap = BitmapFactory.decodeStream(instream, null, options);
        //close out stuff
        try {
            instream.close();
            bufHttpEntity.consumeContent();
            entity.consumeContent();
        } finally {
            instream = null;
            bufHttpEntity = null;
            entity = null;
        }
    } catch (Exception x) {
        Log.e("HttpFetch.fetchBitmap", imageAddress, x);
    } finally {
        httpclient = null;
    }
    return (bitmap);
}

From source file:lh.api.showcase.server.util.HttpQueryUtils.java

public static String executeQuery(URI uri, ApiAuth apiAuth, HasProxySettings proxySetting,
        HttpClientFactory httpClientFactory, final int maxRetries) throws HttpErrorResponseException {

    //logger.info("uri: " + uri.toString());

    AtomicInteger tryCounter = new AtomicInteger(0);
    while (true) {

        CloseableHttpClient httpclient = httpClientFactory.getHttpClient(proxySetting);
        HttpGet httpGet = new HttpGet(uri);
        httpGet.addHeader("Authorization", apiAuth.getAuthHeader());
        httpGet.addHeader("Accept", "application/json");

        //logger.info("auth: " + apiAuth.getAuthHeader()) ;
        //logger.info("query: " + httpGet.toString());

        CloseableHttpResponse response = null;
        try {/*from  w w  w. jav  a2s. c  o m*/
            response = httpclient.execute(httpGet);
            StatusLine status = response.getStatusLine();
            BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity());
            String json = IOUtils.toString(entity.getContent(), "UTF8");
            EntityUtils.consume(entity);
            //logger.info("response: " + json);

            // check for errors
            if (status != null && status.getStatusCode() > 299) {
                if (status.getStatusCode() == 401) {
                    // token has probably expired
                    logger.info("Authentication Error. Token will be refreshed");
                    if (tryCounter.getAndIncrement() < maxRetries) {
                        if (apiAuth.updateAccessToken()) {
                            logger.info("Token successfully refreshed");
                            // we retry with the new token
                            logger.info("Retry number " + tryCounter.get());
                            continue;
                        }
                    }
                }
                throw new HttpErrorResponseException(status.getStatusCode(), status.getReasonPhrase(), json);
            }
            return json;
        } catch (IOException e) {
            logger.severe(e.getMessage());
            break;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                logger.log(Level.SEVERE, e.getMessage());
            }
        }
    }
    return null;
}

From source file:com.othermedia.exampleasyncimage.AsyncImageDemo.java

public static String stringFromURL(String address) throws Exception {

    StringBuilder result = new StringBuilder();
    URL url = new URL(address);
    HttpGet httpRequest = new HttpGet(url.toURI());
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
    InputStream input = bufHttpEntity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));

    String line;//from  ww w  .  j  ava 2s  .com
    while ((line = reader.readLine()) != null) {
        result.append(line);
    }
    reader.close();

    return result.toString();
}

From source file:com.nimpres.android.web.APIContact.java

/**
 * Downloads the presentation dps file/*from   w ww  .  j  a  v  a 2  s  .  co m*/
 * 
 * @param id
 * @param password
 * @return
 */
public static InputStream downloadPresentation(int presID, String presPass) {
    InputStream downloadedDps = null;
    String response = "";
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("pres_id", String.valueOf(presID)));
    params.add(new BasicNameValuePair("pres_password", presPass));
    HttpEntity resEntity = apiPostRequest(NimpresSettings.API_DOWNLOAD_PRESENTATION, params);
    try {
        BufferedHttpEntity buffEnt = new BufferedHttpEntity(resEntity); // Added for file download
        downloadedDps = buffEnt.getContent();
        response = new String(EntityUtils.toString(buffEnt).trim());
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (!response.equals(NimpresSettings.API_RESPONSE_NEGATIVE) && response != "") {
        Log.d("APIContact", "api download success");
        return downloadedDps;
    } else {
        Log.d("APIContact", "api download failed");
        return null;
    }
}

From source file:com.mongolduu.android.ng.misc.HttpConnector.java

private static Bitmap processBitmapEntity(HttpEntity entity) throws IOException {
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
    bufHttpEntity.consumeContent();/*  w  w  w.java2s  .  com*/
    return bm;
}

From source file:com.mongolduu.android.ng.misc.HttpConnector.java

private static byte[] processByteArrayEntity(HttpEntity entity) throws IOException {
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    byte[] buffer = Utils.copyStreamToByteArray(bufHttpEntity.getContent());
    bufHttpEntity.consumeContent();//from  w  ww.  j av  a2  s.co  m
    return buffer;
}

From source file:com.nloko.android.Utils.java

public static InputStream downloadPictureAsStream(String url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("url");
    }//w w  w.j a v  a  2s.co  m

    HttpClient httpclient = null;
    InputStream stream = null;
    try {
        HttpParams params = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        HttpConnectionParams.setConnectionTimeout(params, 5000);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setSoTimeout(params, 10000);

        httpclient = new DefaultHttpClient(params);
        HttpGet httpget = new HttpGet(url);

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            BufferedHttpEntity buff = new BufferedHttpEntity(entity);
            stream = buff.getContent();
            //   image = BitmapFactory.decodeStream(stream);
        }
    } catch (IOException ex) {
        Log.e(null, android.util.Log.getStackTraceString(ex));
        throw ex;
    } finally {
        try {
            if (httpclient != null) {
                httpclient.getConnectionManager().shutdown();
            }
            if (stream != null) {
                stream.close();
            }
        } catch (Exception e) {
        }
    }

    return stream;
}