【发布时间】: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