2013-10-29 112 views
3

試圖讓這個簡單的下載並保存圖像工作,但我不斷收到連接超時異常。Android連接超時在AsyncTask

據我所知網址應該工作

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

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

任何幫助都會很棒。

+1

您是否設置了訪問互聯網的權限? –

+0

確實,從清單 –

+0

添加權限嘗試使用defaulthttpconnection,然後設置連接超時。可能是由於設備網絡連接延遲發生。 –

回答

3

我已測試過您的代碼,並且它可以成功運行。我建議你檢查一下你的網絡連接。

+0

所以它的確如此......它必須與我測試它的代理有關。謝謝 –