【问题标题】:AndroidHttpClient is deprecatedAndroidHttpClient 已弃用
【发布时间】:2016-03-27 08:54:26
【问题描述】:

好吧,我的 ImageDownloader 类被新的 SDK 破坏了。我现在将如何进行这项工作?我是否有可能只替换以下一些类而不对我的代码进行重大更改?

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;
    }

【问题讨论】:

  • 究竟是什么坏了,你现在收到什么错误?
  • 你为什么使用AndroidHttpClient?它确实很古老,多年来一直没有被推荐。仅在最早的 Android 版本中。你应该早就换掉了。无论如何,为什么要大惊小怪?这只是一种方法。重写它应该很简单。到目前为止你尝试了什么?为什么你不能自己做?

标签: java android http httpconnection androidhttpclient


【解决方案1】:

你可以用这个:

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

或在 gradle 文件中:

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

或者您可以更新代码并像这样使用HttpURLConnection:

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());

希望对你有帮助!!!

【讨论】:

    【解决方案2】:

    您可以从以下位置获取 inputStream:

    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();
        }
    }
    

    然后从中获取位图。

    【讨论】:

      【解决方案3】:

      你也可以探索Retrofithtttp客户端

      【讨论】:

        猜你喜欢
        • 2015-11-12
        • 2014-02-12
        • 1970-01-01
        • 2016-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        • 1970-01-01
        相关资源
        最近更新 更多