在Android的AsyncTask的连接超时 [英] Android Connection Timeout on AsyncTask

查看:254
本文介绍了在Android的AsyncTask的连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to get this simple download and save image to work but i keep getting a Connection Timeout Exception.

据我所知网址应该工作

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();
        }
    }
}

和继承人的outoput

And heres the outoput

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"/>

任何帮助将是巨大的。

Any help would be great.

推荐答案

我测试过你的code和它的作品成功。我建议你​​检查你的互联网连接。

I've tested your code and it works successfully. I suggest you to check your internet connection.

这篇关于在Android的AsyncTask的连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆