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:io.uploader.drive.drive.largefile.DriveResumableUpload.java

private String getResumableUploadUpdateUri(DriveUtils.HasId fileId) throws IOException {
    String getUri = "https://www.googleapis.com/upload/drive/v2/files";
    if (useOldApi) {
        StringBuilder sb = new StringBuilder();
        sb.append("https://docs.google.com/feeds/default/private/full/");
        sb.append(fileId.getId());/*w  ww .  j  a  va  2  s.  c  om*/
        getUri = sb.toString();
    } else {
        // TODO: new api
        // ...
        throw new IllegalStateException("Not implemented");
    }

    CloseableHttpClient httpclient = null;
    CloseableHttpResponse response = null;
    String putUri = null;
    try {
        httpclient = getHttpClient();
        HttpGet httpGet = new HttpGet(getUri);
        httpGet.addHeader("Authorization", auth.getAuthHeader());
        httpGet.addHeader("GData-Version", "3");
        response = httpclient.execute(httpGet);

        BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity());
        String contents = IOUtils.toString(entity.getContent(), "UTF8");

        String strBegin = "#resumable-edit-media";
        String uriBegin = "href='";
        String uriEnd = "'/>";
        int index = contents.indexOf(strBegin);
        if (index < 0) {
            return null;
        }
        contents = contents.substring(index + strBegin.length());

        index = contents.indexOf(uriBegin);
        if (index < 0) {
            return null;
        }
        contents = contents.substring(index + uriBegin.length());

        index = contents.indexOf(uriEnd);
        if (index < 0) {
            return null;
        }
        contents = contents.substring(0, index);
        EntityUtils.consume(response.getEntity());
        putUri = contents;
        //logger.info("Upload update uri: " + putUri);
        return putUri;
    } finally {
        if (response != null) {
            response.close();
        }
        if (httpclient != null) {
            httpclient.close();
        }
    }
}

From source file:org.mozilla.gecko.favicons.LoadFaviconTask.java

private Bitmap downloadFavicon(URI targetFaviconURI) {
    if (targetFaviconURI == null) {
        return null;
    }//from   ww w  .j a v  a 2 s  .com

    // Only get favicons for HTTP/HTTPS.
    String scheme = targetFaviconURI.getScheme();
    if (!"http".equals(scheme) && !"https".equals(scheme)) {
        return null;
    }

    Bitmap image = null;

    // skia decoder sometimes returns null; workaround is to use BufferedHttpEntity
    // http://groups.google.com/group/android-developers/browse_thread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot
    try {
        // Try the URL we were given.
        HttpResponse response = tryDownload(targetFaviconURI);
        if (response == null) {
            return null;
        }

        HttpEntity entity = response.getEntity();
        if (entity == null) {
            return null;
        }

        BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
        InputStream contentStream = null;
        try {
            contentStream = bufferedEntity.getContent();
            image = BitmapUtils.decodeStream(contentStream);
            contentStream.close();
        } finally {
            if (contentStream != null) {
                contentStream.close();
            }
        }
    } catch (Exception e) {
        Log.e(LOGTAG, "Error reading favicon", e);
    }

    return image;
}

From source file:semanticweb.hws14.movapp.activities.MovieDetail.java

private void setData(final MovieDet movie) {
    Thread picThread = new Thread(new Runnable() {
        public void run() {
            try {
                ImageView img = (ImageView) findViewById(R.id.imageViewMovie);
                URL url = new URL(movie.getPoster());
                HttpGet httpRequest;//from w w  w .ja v a2  s .  c  om

                httpRequest = new HttpGet(url.toURI());

                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httpRequest);

                HttpEntity entity = response.getEntity();
                BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                InputStream input = b_entity.getContent();

                Bitmap bitmap = BitmapFactory.decodeStream(input);

                img.setImageBitmap(bitmap);

            } catch (Exception ex) {
                Log.d("MovieDetail Picture Error ", ex.toString());
            }
        }
    });
    picThread.start();

    if (!"".equals(movie.getTitle())) {
        TextView movieTitle = (TextView) findViewById(R.id.tvMovieTitle);
        movieTitle.setText(movie.getTitle());
        movieTitle.setVisibility(View.VISIBLE);
    }

    if (!"".equals(movie.getPlot())) {
        TextView moviePlot = (TextView) findViewById(R.id.tvPlot);
        moviePlot.setText(movie.getPlot());
        moviePlot.setVisibility(View.VISIBLE);
    }

    TextView ageRestriction = (TextView) findViewById(R.id.tvAgeRestriction);
    TextView arHc = (TextView) findViewById(R.id.tvAgeRestrictionHC);
    String aR = String.valueOf(movie.getRated());

    //Decode the ageRestriction
    if (!aR.equals("")) {
        ageRestriction.setVisibility(View.VISIBLE);
        arHc.setVisibility(View.VISIBLE);
        if (aR.equals("X")) {
            ageRestriction.setText("18+");
        } else if (aR.equals("R")) {
            ageRestriction.setText("16+");
        } else if (aR.equals("M")) {
            ageRestriction.setText("12+");
        } else if (aR.equals("G")) {
            ageRestriction.setText("6+");
        } else {
            ageRestriction.setVisibility(View.GONE);
            arHc.setVisibility(View.GONE);
        }
    }

    TextView ratingCount = (TextView) findViewById(R.id.tvMovieRatingCount);
    TextView ratingCountHc = (TextView) findViewById(R.id.tvMovieRatingCountHC);
    String ratingCountText = movie.getVoteCount();
    ratingCount.setText(ratingCountText);
    manageEmptyTextfields(ratingCountHc, ratingCount, ratingCountText, false);

    if (!"".equals(movie.getImdbRating())) {
        TextView movieRating = (TextView) findViewById(R.id.tvMovieRating);
        if (movie.getImdbRating().equals("0 No Rating")) {
            movieRating.setText("No Rating");
        } else if (movie.getImdbRating().equals("0 No Data")) {
            movieRating.setText("");
        } else {
            movieRating.setText(movie.getImdbRating() + "/10");
        }
        movieRating.setVisibility(View.VISIBLE);
        findViewById(R.id.tvMovieRatingHC).setVisibility(View.VISIBLE);
    }

    TextView metaScore = (TextView) findViewById(R.id.tvMetaScore);
    TextView metaScoreHc = (TextView) findViewById(R.id.tvMetaScoreHC);
    String metaSoreText = String.valueOf(movie.getMetaScore());
    metaScore.setText(metaSoreText + "/100");
    manageEmptyTextfields(metaScoreHc, metaScore, metaSoreText, false);

    TextView tvGenreHc = (TextView) findViewById(R.id.tvGenreHC);
    TextView genre = (TextView) findViewById(R.id.tvGenre);
    String genreText = String.valueOf(movie.createTvOutOfList(movie.getGenres()));
    genre.setText(genreText);
    manageEmptyTextfields(tvGenreHc, genre, genreText, true);

    TextView releaseYear = (TextView) findViewById(R.id.tvReleaseYear);
    TextView releaseYearHc = (TextView) findViewById(R.id.tvReleaseYearHC);
    String releaseYearText = String.valueOf(movie.getReleaseYear());
    releaseYear.setText(releaseYearText);
    manageEmptyTextfields(releaseYearHc, releaseYear, releaseYearText, true);

    TextView runtime = (TextView) findViewById(R.id.tvRuntime);
    TextView runTimeHc = (TextView) findViewById(R.id.tvRuntimeHC);
    String runtimeText = "";
    try {
        runtimeText = runtimeComputation(Float.parseFloat(movie.getRuntime()));
    } catch (Exception e) {
        runtimeText = movie.getRuntime();
    }
    runtime.setText(runtimeText);
    manageEmptyTextfields(runTimeHc, runtime, runtimeText, true);

    TextView budget = (TextView) findViewById(R.id.tvBudget);
    TextView budgetHc = (TextView) findViewById(R.id.tvBudgetHC);
    String budgetText = movie.getBudget();
    //Decode the budget
    if (budgetText.contains("E")) {
        BigDecimal myNumber = new BigDecimal(budgetText);
        long budgetLong = myNumber.longValue();
        NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
        budgetText = formatter.format(budgetLong);
        budgetText = String.valueOf(budgetText);
        budgetText = budgetComputation(budgetText);
        budget.setText(budgetText);
        manageEmptyTextfields(budgetHc, budget, budgetText, true);
    }

    TextView awards = (TextView) findViewById(R.id.tvAwards);
    TextView awardsHc = (TextView) findViewById(R.id.tvAwardsHC);
    String awardsText = movie.getAwards();
    awards.setText(awardsText);
    manageEmptyTextfields(awardsHc, awards, awardsText, true);

    TextView tvDirHc = (TextView) findViewById(R.id.tvDirectorsHC);
    TextView directors = (TextView) findViewById(R.id.tvDirectors);
    String directorText = String.valueOf(movie.createTvOutOfList(movie.getDirectors()));
    directors.setText(directorText);
    manageEmptyTextfields(tvDirHc, directors, directorText, true);

    TextView tvWriterHc = (TextView) findViewById(R.id.tvWritersHC);
    TextView writers = (TextView) findViewById(R.id.tvWriters);
    String writerText = String.valueOf(movie.createTvOutOfList(movie.getWriters()));
    writers.setText(writerText);
    manageEmptyTextfields(tvWriterHc, writers, writerText, true);

    TextView tvActorsHc = (TextView) findViewById(R.id.tvActorsHC);
    TextView actors = (TextView) findViewById(R.id.tvActors);
    String actorText = String.valueOf(movie.createTvOutOfList(movie.getActors()));
    actors.setText(actorText);
    manageEmptyTextfields(tvActorsHc, actors, actorText, true);
    colorIt(actors);

    if (movie.getActors().size() > 0) {
        btnActorList.setVisibility(View.VISIBLE);
    }

    btnRandomRelatedMovies.setVisibility(View.VISIBLE);
    btnRelatedMovies.setVisibility(View.VISIBLE);

    TextView tvRolesHc = (TextView) findViewById(R.id.tvRolesHC);
    TextView roles = (TextView) findViewById(R.id.tvRoles);
    ArrayList<String> roleList = movie.getRoles();

    if (roleList.size() > 0) {
        roles.setVisibility(View.VISIBLE);
        tvRolesHc.setVisibility(View.VISIBLE);
        roles.setText(String.valueOf(movie.createTvOutOfList(roleList)));
    }

    TextView wikiAbstract = (TextView) findViewById(R.id.tvWikiAbstract);
    wikiAbstract.setText(movie.getWikiAbstract());
    if (!"".equals(movie.getImdbId())) {
        btnImdbPage.setVisibility(View.VISIBLE);
    }
    if (!"".equals(movie.getWikiAbstract())) {
        btnSpoiler.setVisibility(View.VISIBLE);
    }
    try {
        picThread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    setProgressBarIndeterminateVisibility(false);
}

From source file:com.neusou.bioroid.image.ImageLoader.java

public Bitmap loadImage(final String imageUri, boolean immediately) {
    if (imageUri == null || imageUri.equals("null")) {
        return null;
    }// ww w .  j  ava  2  s  .  c om

    final String url;
    url = imageUri;

    if (url == null || url.compareTo("null") == 0) {
        return null;
    }

    Bitmap dcached;
    dcached = mBitmapMap.get(url);

    if (dcached != null || immediately) {
        if (dcached == null) {
            mBitmapMap.remove(url);
        }
        return dcached;
    }

    URL imageUrl = null;

    try {
        imageUrl = new URL(imageUri);
    } catch (MalformedURLException e) {
        return null;
    }

    File imageDir = new File(mCacheDirectoryPath);
    String bigInt = computeDigest(url);
    File cachedImageFile = new File(imageDir, bigInt);
    String ess = Environment.getExternalStorageState();
    boolean canReadImageCacheDir = false;

    if (ess.equals(Environment.MEDIA_MOUNTED)) {
        if (!imageDir.canRead()) {
            boolean createSuccess = false;
            synchronized (imageDir) {
                createSuccess = imageDir.mkdirs();
            }
            canReadImageCacheDir = createSuccess && imageDir.canRead();
        }
        canReadImageCacheDir = imageDir.canRead();
        if (cachedImageFile.canRead()) {
            try {
                Bitmap cachedBitmap = getCachedBitmap(imageUrl, cachedImageFile);
                if (cachedBitmap != null) {
                    mBitmapMap.put(imageUri, cachedBitmap);
                    return cachedBitmap;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    HttpGet httpRequest = null;
    try {
        httpRequest = new HttpGet(imageUrl.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return null;
    }

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = null;
    try {
        response = (HttpResponse) httpclient.execute(httpRequest);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = null;
    try {
        bufHttpEntity = new BufferedHttpEntity(entity);
        final int bufferSize = 1024 * 50;
        BufferedInputStream imageBis = new BufferedInputStream(bufHttpEntity.getContent(), bufferSize);
        long contentLength = bufHttpEntity.getContentLength();
        Bitmap decodedBitmap = decode(imageBis);
        try {
            imageBis.close();
        } catch (IOException e1) {
        }
        encode(decodedBitmap, cachedImageFile.toURL(), CompressFormat.PNG, mImageCacheQuality);
        Calendar cal = Calendar.getInstance();
        long currentTime = cal.getTime().getTime();
        if (mUseCacheDatabase) {
            mCacheRecordDbHelper.insertCacheRecord(mDb, cachedImageFile.toURL(), currentTime, contentLength);
        }
        if (decodedBitmap != null) {
            mBitmapMap.put(url, decodedBitmap);
        }
        return decodedBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.netcompss.ffmpeg4android_client.BaseVideo.java

@SuppressWarnings("unused")
private String reporteds(String path) {

    ExifInterface exif = null;//from w  ww.  j av a 2 s .  c o m
    try {
        exif = new ExifInterface(path);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    Matrix matrix = new Matrix();
    if (orientation == 6) {
        matrix.postRotate(90);
    } else if (orientation == 3) {
        matrix.postRotate(180);
    } else if (orientation == 8) {
        matrix.postRotate(270);
    }

    if (path != null) {

        if (path.contains("http")) {
            try {
                URL url = new URL(path);
                HttpGet httpRequest = null;

                httpRequest = new HttpGet(url.toURI());

                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

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

                Bitmap bitmap = BitmapFactory.decodeStream(input);
                input.close();
                return getPath(bitmap);
            } catch (MalformedURLException e) {
                Log.e("ImageActivity", "bad url", e);
            } catch (Exception e) {
                Log.e("ImageActivity", "io error", e);
            }
        } else {

            Options options = new Options();
            options.inSampleSize = 2;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(context.getResources(), srcBgId, options);
            options.inJustDecodeBounds = false;
            options.inSampleSize = calculateInSampleSize(options, w, h);
            Bitmap unbgbtmp = BitmapFactory.decodeResource(context.getResources(), srcBgId, options);
            Bitmap unrlbtmp = ScalingUtilities.decodeFile(path, w, h, ScalingLogic.FIT);
            unrlbtmp.recycle();
            Bitmap rlbtmp = null;
            if (unrlbtmp != null) {
                rlbtmp = ScalingUtilities.createScaledBitmap(unrlbtmp, w, h, ScalingLogic.FIT);
            }
            if (unbgbtmp != null && rlbtmp != null) {
                Bitmap bgbtmp = ScalingUtilities.createScaledBitmap(unbgbtmp, w, h, ScalingLogic.FIT);
                Bitmap newscaledBitmap = ProcessingBitmapTwo(bgbtmp, rlbtmp);
                unbgbtmp.recycle();
                return getPath(newscaledBitmap);
            }
        }
    }
    return path;

}

From source file:com.netcompss.ffmpeg4android_client.BaseWizard.java

@SuppressWarnings("unused")
private String reporteds(String path) {

    ExifInterface exif = null;//w ww .  j a  va2 s .  c om
    try {
        exif = new ExifInterface(path);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    Matrix matrix = new Matrix();
    if (orientation == 6) {
        matrix.postRotate(90);
    } else if (orientation == 3) {
        matrix.postRotate(180);
    } else if (orientation == 8) {
        matrix.postRotate(270);
    }

    if (path != null) {

        if (path.contains("http")) {
            try {
                URL url = new URL(path);
                HttpGet httpRequest = null;

                httpRequest = new HttpGet(url.toURI());

                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

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

                Bitmap bitmap = BitmapFactory.decodeStream(input);
                input.close();
                return getPath(bitmap);
            } catch (MalformedURLException e) {
                Log.e("ImageActivity", "bad url", e);
            } catch (Exception e) {
                Log.e("ImageActivity", "io error", e);
            }
        } else {
            Options options = new Options();
            options.inSampleSize = 2;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(getApplicationContext().getResources(), srcBgId, options);
            options.inJustDecodeBounds = false;
            options.inSampleSize = calculateInSampleSize(options, w, h);
            Bitmap unbgbtmp = BitmapFactory.decodeResource(getApplicationContext().getResources(), srcBgId,
                    options);
            Bitmap unrlbtmp = ScalingUtilities.decodeFile(path, w, h, ScalingLogic.FIT);
            unrlbtmp.recycle();
            Bitmap rlbtmp = null;
            if (unrlbtmp != null) {
                rlbtmp = ScalingUtilities.createScaledBitmap(unrlbtmp, w, h, ScalingLogic.FIT);
            }
            if (unbgbtmp != null && rlbtmp != null) {
                Bitmap bgbtmp = ScalingUtilities.createScaledBitmap(unbgbtmp, w, h, ScalingLogic.FIT);
                Bitmap newscaledBitmap = ProcessingBitmapTwo(bgbtmp, rlbtmp);
                unbgbtmp.recycle();
                return getPath(newscaledBitmap);
            }
        }
    }
    return path;

}

From source file:com.eleybourn.bookcatalogue.utils.Utils.java

/**
 * Given a URL, get an image and save to a file, optionally appending a suffic to the file.
 * /*  w  ww. ja va 2s .  c o  m*/
 * @param urlText         Image file URL
 * @param filenameSuffix   Suffix to add
 *
 * @return   Downloaded filespec
 */
static public String saveThumbnailFromUrl(String urlText, String filenameSuffix) {
    // Get the URL
    URL u;
    try {
        u = new URL(urlText);
    } catch (MalformedURLException e) {
        Logger.logError(e);
        return "";
    }
    // Turn the URL into an InputStream
    InputStream in = null;
    try {
        HttpGet httpRequest = null;

        httpRequest = new HttpGet(u.toURI());

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
        in = bufHttpEntity.getContent();

        // The defaut URL fetcher does not cope well with pages that have not content
        // header (including goodreads images!). So use the more advanced one.
        //c = (HttpURLConnection) u.openConnection();
        //c.setConnectTimeout(30000);
        //c.setRequestMethod("GET");
        //c.setDoOutput(true);
        //c.connect();
        //in = c.getInputStream();
    } catch (IOException e) {
        Logger.logError(e);
        return "";
    } catch (URISyntaxException e) {
        Logger.logError(e);
        return "";
    }

    // Get the output file
    File file = CatalogueDBAdapter.getTempThumbnail(filenameSuffix);
    // Save to file
    saveInputToFile(in, file);
    // Return new file path
    return file.getAbsolutePath();
}

From source file:net.networksaremadeofstring.rhybudd.ZenossAPI.java

public Drawable GetGraph(String urlString) throws IOException, URISyntaxException {
    HttpGet httpRequest = new HttpGet(new URL(ZENOSS_INSTANCE + urlString).toURI());
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    final long contentLength = bufHttpEntity.getContentLength();
    //Log.e("GetGraph",Long.toString(contentLength));
    if (contentLength >= 0) {
        InputStream is = bufHttpEntity.getContent();
        Bitmap bitmap = BitmapFactory.decodeStream(is);

        /*ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 10, out);
                // w  w w. j ava  2s .  co m
        return new BitmapDrawable(BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())));*/
        is.close();
        return new BitmapDrawable(bitmap);
    } else {
        return null;
    }
}