【问题标题】:Android Connection Timeout on AsyncTaskAsyncTask 上的 Android 连接超时
【发布时间】:2013-10-29 12:44:58
【问题描述】:

试图让这个简单的下载和保存图像工作,但我不断收到连接超时异常。

据我所知,网址应该可以工作

new DownloadImageTask();



private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {


    protected Bitmap doInBackground(String... urls) {

        String urldisplay = "http://masterzangetsu.eu/Apps/BandWallpapers/Blink182/1_thumbnail.png";
        Bitmap bitmap = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            bitmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }

    protected void onPostExecute(Bitmap bitmap) {

        File sdCardDirectory = Environment.getExternalStorageDirectory();
        File image = new File(sdCardDirectory, "test.png");

        boolean success = false;

        // Encode the file as a PNG image.
        FileOutputStream outStream;
        try {

            outStream = new FileOutputStream(image);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
            /* 100 to keep full quality of the image */

            outStream.flush();
            outStream.close();
            success = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (success) {
            Toast.makeText(getApplicationContext(), "Image saved with success",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Error during image saving", Toast.LENGTH_LONG).show();
        }
    }
}

这是输出

10-29 12:33:36.075: W/System.err(817): java.net.ConnectException: failed to connect to     masterzangetsu.eu/91.208.99.12 (port 80): connect failed: ETIMEDOUT (Connection timed out)
10-29 12:33:36.075: W/System.err(817):  at libcore.io.IoBridge.connect(IoBridge.java:114)
10-29 12:33:36.095: W/System.err(817):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-29 12:33:36.095: W/System.err(817):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-29 12:33:36.095: W/System.err(817):  at java.net.Socket.connect(Socket.java:842)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-29 12:33:36.095: W/System.err(817):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
10-29 12:33:36.105: W/System.err(817):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
10-29 12:33:36.115: W/System.err(817):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
10-29 12:33:36.115: W/System.err(817):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
10-29 12:33:36.115: W/System.err(817):  at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
10-29 12:33:36.115: W/System.err(817):  at com.koushikdutta.urlimageviewhelper.HttpUrlDownloader$1.doInBackground(HttpUrlDownloader.java:51)
10-29 12:33:36.115: W/System.err(817):  at com.koushikdutta.urlimageviewhelper.HttpUrlDownloader$1.doInBackground(HttpUrlDownloader.java:1)
10-29 12:33:36.125: W/System.err(817):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-29 12:33:36.125: W/System.err(817):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-29 12:33:36.125: W/System.err(817):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-29 12:33:36.136: W/System.err(817):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-29 12:33:36.136: W/System.err(817):  at java.lang.Thread.run(Thread.java:856)
10-29 12:33:36.145: W/System.err(817): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
10-29 12:33:36.165: W/System.err(817):  at libcore.io.Posix.connect(Native Method)
10-29 12:33:36.165: W/System.err(817):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-29 12:33:36.165: W/System.err(817):  at     libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-29 12:33:36.165: W/System.err(817):  at libcore.io.IoBridge.connect(IoBridge.java:112)
10-29 12:33:36.165: W/System.err(817):  ... 21 more

完整输出http://pastie.org/8439940

和权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

任何帮助都会很棒。

【问题讨论】:

  • 您是否设置了访问互联网的权限?
  • 确实,添加了清单中的权限
  • 尝试使用defaulthttpconnection,然后设置连接超时。可能是由于设备网络连接导致延迟发生。

标签: java android bitmap android-asynctask


【解决方案1】:

我已经测试了您的代码,它可以成功运行。我建议您检查您的互联网连接。

【讨论】:

  • 确实如此……它一定与我测试它的代理有关。谢谢
猜你喜欢
  • 2012-11-14
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 2021-06-01
  • 2012-10-13
  • 2017-09-05
相关资源
最近更新 更多