Java 图像不';不出现

Java 图像不';不出现,java,android,bitmap,android-asynctask,Java,Android,Bitmap,Android Asynctask,我正在尝试使用异步任务解码从internet下载的图像。问题是图像有时加载,有时不加载。URL上的图像始终存在 以下是我的asycn任务: private class JSONIconWeatherTask extends AsyncTask<String, Void, byte[]> { @Override protected byte[] doInBackground(String... params) { byte[] data = null

我正在尝试使用异步任务解码从internet下载的图像。问题是图像有时加载,有时不加载。URL上的图像始终存在

以下是我的asycn任务:

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);
        }
    }
私有类JSONIconWeatherTask扩展了异步任务{
@凌驾
受保护字节[]doInBackground(字符串…参数){
字节[]数据=null;
试一试{
//让我们检索图标
数据=((新的WeatherHttpClient()).getImage(参数[0]);
}捕获(例外e){
e、 printStackTrace();
}
返回数据;
}
@凌驾
受保护的void onPostExecute(字节[]数据){
super.onPostExecute(数据);
如果(数据!=null){
位图img=BitmapFactory.decodeByteArray(数据,0,数据.长度);
设置图像位图(img);
}
}
}

这是我的日志:

09-29 15:53:14.590:W/System.err(14373):java.net.ConnectException:无法连接到/127.0.0.1(端口81):连接失败:ECONREFUSE(连接被拒绝) 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):位于java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 09-29 15:53:14.590:W/System.err(14373):位于java.net.Socket.connect(Socket.java:842) 09-29 15:53:14.590:W/System.err(14373):位于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.err(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):位于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):位于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:connect failed:ECONNREFUSED(连接被拒绝) 09-29 15:53:14.600:W/System.err(14373):位于libcore.io.Posix.connect(本机方法) 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:W/System.err(14373):位于libcore.io.IoBridge.connect(IoBridge.java:112)

“连接被拒绝”表示您尝试连接的服务器(此处:localhost)在您尝试连接的端口(此处:81)上没有任何侦听内容

localhost这个名称指的是运行代码的设备,即android设备或模拟器。除非您同时运行的应用程序在同一设备的端口81上接受连接,否则您连接到的地址是错误的


如果您正在开发计算机上运行服务器,并尝试从模拟器连接到服务器,则应使用10.0.2.2而不是localhost。请参见

我正在手机上运行它,如何修复它?请使用正确的地址。例如,如果您使用WiFi,则家庭路由器为您的机器分配的IP(假设服务器位于您的开发机器上)