1

Well, my ImageDownloader class is broken with the new Sdk. How would I go about making this work now? Any possibility I could just replace some of the following classes without making major changes to my code?

static Bitmap downloadBitmap(String url) {
        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent();
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            // Could provide a more explicit error message for IOException or IllegalStateException
            getRequest.abort();
            Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return null;
    }
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • 1
    Exactly what is broken and what errors are you now receiving? – Sam Dec 21 '15 at 10:01
  • 1
    Why did you ever use the `AndroidHttpClient`? It is literally ancient, it hasn't been recommended for years. Only in the very earliest of Android versions. You should have replaced this a long time ago. Anyway, why the big fuss? It's just one method. Rewriting it should be quite simple. What did you try so far? Why couldn't you do it yourself? – Xaver Kapeller Dec 21 '15 at 10:16

3 Answers3

1

You can use this:

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

or this in the gradle file:

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'
    useLibrary 'org.apache.http.legacy' //<-this line only

or you can just update the code and use the HttpURLConnection like this:

URL url = new URL("your_url");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
setupDataToDB();
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(StringGenerator.queryResults(nameValuePairs));
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
httpURLConnection.connect();
InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());

Hope it helps!!!

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60
1

You can take an inputStream from:

HttpURLConnection urlConnection = null;
try {
    URL url = new URL(path);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setConnectTimeout(CONNECTON_TIMEOUT_MILLISECONDS);
    urlConnection.setReadTimeout(CONNECTON_TIMEOUT_MILLISECONDS);
    inputStream = urlConnection.getInputStream();
    //add code to take bitmap here
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
}

and than take bitmap from it.

Mk.Sl.
  • 2,879
  • 1
  • 11
  • 11
1

You can also explore Retrofit htttp client

hard coder
  • 5,449
  • 6
  • 36
  • 61