我正在尝试使用异步任务解码从 Internet 下载的图像。问题是图像有时会加载,有时不会。URL 上的图像始终存在。
这是我的异步任务:
private class JSONIconWeatherTask extends AsyncTask<String, Void, byte[]> {
@Override
protected byte[] doInBackground(String... params) {
byte[] data = null;
try {
// Let's retrieve the icon
data = ( (new WeatherHttpClient()).getImage(params[0]));
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(byte[] data) {
super.onPostExecute(data);
if (data != null) {
Bitmap img = BitmapFactory.decodeByteArray(data, 0, data.length);
iconWeather.setImageBitmap(img);
}
}
}
这是我的日志:
09-29 15:53:14.590: W/System.err(14373): java.net.ConnectException: 无法连接到 /127.0.0.1(端口 81):连接失败:ECONNREFUSED(连接被拒绝) 09-29 15: 53:14.590: W/System.err(14373): 在 libcore.io.IoBridge.connect(IoBridge.java:114) 09-29 15:53:14.590: W/System.err(14373): 在 java.net .PlainSocketImpl.connect(PlainSocketImpl.java:192) 09-29 15:53:14.590: W/System.err(14373): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 09-29 15:53 :14.590: W/System.err(14373): at java.net.Socket.connect(Socket.java:842) 09-29 15:53:14.590: W/System.err(14373): at libcore.net。 http.HttpConnection.(HttpConnection.java:76) 09-29 15:53:14.590: W/System.err(14373): 在 libcore.net.http.HttpConnection.(HttpConnection.java:50) 09-29 15: 53:14.590:W/System.err(14373):在 libcore.net.http。HttpConnection$Address.connect(HttpConnection.java:340) 09-29 15:53:14.590: W/System.err(14373): 在 libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 09-29 15:53:14.590: W/System.err(14373): 在 libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 09-29 15:53:14.590: W/System.err(14373):在 libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 09-29 15:53:14.590: W/System.err(14373): 在 libcore.net.http.HttpEngine.connect(HttpEngine.java: 311) 09-29 15:53:14.590: W/System.err(14373): 在 libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 09-29 15:53:14.590: W/System。错误(14373):在 libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)09-29 15:53:14.590:W/System.err(14373):在 libcore.net.http.HttpURLConnectionImpl.getResponse (HttpURLConnectionImpl.java:282) 09-29 15:53:14.590: W/System.err(14373): 在 libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 09-29 15:53:14.590: W/ System.err(14373): 在 com.example.weatherforecast.WeatherHttpClient.getImage(WeatherHttpClient.java:116) 09-29 15:53:14.590: W/System.err(14373): 在 com.example.weatherforecast.MainActivity $JSONForecastWeatherTask.doInBackground(MainActivity.java:187) 09-29 15:53:14.590: W/System.err(14373): at com.example.weatherforecast.MainActivity$JSONForecastWeatherTask.doInBackground(MainActivity.java:1) 09- 29 15:53:14.590: W/System.err(14373): 在 android.os.AsyncTask$2.call(AsyncTask.java:287) 09-29 15:53:14.590: W/System.err(14373):在 java.util.concurrent.FutureTask.run(FutureTask.java:234) 09-29 15:53:14.590: W/System.err(14373): 在 android.os。AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 09-29 15:53:14.590: W/System.err(14373): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 09- 29 15:53:14.600: W/System.err(14373): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 09-29 15:53:14.600: W/System.err( 14373): 在 java.lang.Thread.run(Thread.java:856) 09-29 15:53:14.600: W/System.err(14373): 由: libcore.io.ErrnoException: 连接失败: ECONNREFUSED (连接被拒绝)09-29 15:53:14.600: W/System.err(14373): 在 libcore.io.Posix.connect(Native Method) 09-29 15:53:14.600: W/System.err(14373) : 在 libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) 09-29 15:53:14.600: W/System.err(14373): 在 libcore.io.IoBridge.connectErrno(IoBridge.java:127) 09 -29 15:53:14.600:带系统。错误(14373):在 libcore.io.IoBridge.connect(IoBridge.java:112)