简体   繁体   English

图像不出现

[英]Images don't appear

Im trying to decode images downloaded from the internet using an async task. 我试图使用异步任务解码从互联网下载的图像。 The problem is that the images sometimes load and sometimes don't. 问题在于图像有时加载而有时不加载。 The image on the URL is always present. URL上的图像始终存在。

Here is my asycn task: 这是我的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);
        }
    }

} }

And here is my log: 这是我的日志:

09-29 15:53:14.590: W/System.err(14373): java.net.ConnectException: failed to connect to /127.0.0.1 (port 81): connect failed: ECONNREFUSED (Connection refused) 09-29 15:53:14.590: W/System.err(14373): at libcore.io.IoBridge.connect(IoBridge.java:114) 09-29 15:53:14.590: W/System.err(14373): at 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): at libcore.net.http.HttpConnection.(HttpConnection.java:50) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 09-29 15:53:14.590: W/Syst 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):在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 / Syst em.err(14373): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282) 09-29 15:53:14.590: W/System.err(14373): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 09-29 15:53:14.590: W/System.err(14373): at com.example.weatherforecast.WeatherHttpClient.getImage(WeatherHttpClient.java:116) 09-29 15:53:14.590: W/System.err(14373): at com.example.weatherforecast.MainActivity$JSONForecastWeath em.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 ):at com.example.weatherforecast.WeatherHttpClient.getImage(WeatherHttpClient.java:116)09-29 15:53:14.590:W / System.err(14373):at com.example.weatherforecast.MainActivity $ JSONForecastWeath erTask.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): at android.os.AsyncTask$2.call(AsyncTask.java:287) 09-29 15:53:14.590: W/System.err(14373): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 09-29 15:53:14.590: W/System.err(14373): at 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): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 09-29 15:53:14.600: W/System.err(14373): at java.lang.Thread.run(Thread.java:856) 09-29 15:53:14.600: W/System.err(14373): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused) 09-29 15:53:14.600: W/System.err(14373): at libcore. erTask.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):at android.os.AsyncTask $ 2.call(AsyncTask.java:287)09-29 15:53:14.590:W / System.err(14373):at java.util.concurrent.FutureTask.run(FutureTask.java:234)09-29 15:53:14.590:W / System.err(14373):at 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 /系统。 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): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) 09-29 15:53:14.600: W/System.err(14373): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 09-29 15:53:14.600: W/System.err(14373): at libcore.io.IoBridge.connect(IoBridge.java:112) 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)

"Connection refused" means that the server you try to connect (here: localhost) to doesn't have anything listening on the port you try to connect to (here: 81). “拒绝连接”表示您尝试连接的服务器(在此为localhost)在尝试连接的端口上没有任何监听(在此为81)。

The name localhost refers to the device the code runs on, that is the android device or emulator. 名称localhost是指代码在其上运行的设备,即android设备或模拟器。 Unless you are also running an application that accepts connections on port 81 on the same device, you are connecting to the wrong address. 除非您也正在运行在同一设备上的端口81上接受连接的应用程序,否则您将连接到错误的地址。

If you are running a server on your development machine and try to connect there from the emulator you should use 10.0.2.2 instead of localhost. 如果在开发计算机上运行服务器,并尝试从仿真器连接到服务器,则应使用10.0.2.2而不是localhost。 See why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client 了解为什么我们使用10.0.2.2连接到本地Web服务器,而不是在android客户端中使用计算机ip地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM