好吧,我的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;
}发布于 2015-12-21 10:15:06
你可以用这个:
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());希望能帮上忙!
发布于 2015-12-21 10:17:37
您可以从以下位置获取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();
}
}而不是从里面取位图。
发布于 2015-12-21 10:47:52
您还可以探索改造 htttp客户端。
https://stackoverflow.com/questions/34393031
复制相似问题