2011-06-01 54 views
0

我有以下代碼以在通知系統中下載文件。但它似乎沒有工作......我不知道爲什麼,一切看起來都不錯。調試器給我一個未知的主機錯誤?未找到主機錯誤AsyncTask

06-02 00:21:15.308: WARN/System.err(4115): java.net.UnknownHostException: phobos.emuparadise.org 
06-02 00:21:15.308: WARN/System.err(4115):  at java.net.InetAddress.lookupHostByName(InetAddress.java:506) 
06-02 00:21:15.308: WARN/System.err(4115):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 
06-02 00:21:15.308: WARN/System.err(4115):  at java.net.InetAddress.getAllByName(InetAddress.java:256) 
06-02 00:21:15.308: WARN/System.err(4115):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69) 
06-02 00:21:15.308: WARN/System.err(4115):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
06-02 00:21:15.308: WARN/System.err(4115):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322) 
06-02 00:21:15.308: WARN/System.err(4115):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
06-02 00:21:15.308: WARN/System.err(4115):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 
06-02 00:21:15.308: WARN/System.err(4115):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 
06-02 00:21:15.312: WARN/System.err(4115):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 
06-02 00:21:15.312: WARN/System.err(4115):  at co.ezeh.android.romget.ui.ListActivity$RomDownloadTask.doInBackground(ListActivity.java:139) 
06-02 00:21:15.312: WARN/System.err(4115):  at co.ezeh.android.romget.ui.ListActivity$RomDownloadTask.doInBackground(ListActivity.java:1) 
06-02 00:21:15.312: WARN/System.err(4115):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
06-02 00:21:15.316: WARN/System.err(4115):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
06-02 00:21:15.316: WARN/System.err(4115):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
06-02 00:21:15.316: WARN/System.err(4115):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
06-02 00:21:15.316: WARN/System.err(4115):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
06-02 00:21:15.320: WARN/System.err(4115):  at java.lang.Thread.run(Thread.java:1019) 

這也是我想使用,我不能看到它雖然任何錯誤的類...

private class RomDownloadTask extends AsyncTask<RomDataSet, Integer, String> { 

    private PendingIntent pendingIntent; 
    private Notification notification; 
    private NotificationManager notificationManager; 

    @Override 
    protected String doInBackground(RomDataSet... params) { 
     try { 
      //set the download URL, a url that points to a file on the internet 
      //this is the file to be downloaded 
      URL url = new URL(params[0].url); 

      //create the new connection 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      //set up some things on the connection 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 

      //and connect! 
      urlConnection.connect(); 

      //set the path where we want to save the file 
      //in this case, going to save it on the root directory of the 
      //sd card. 
      File SDCardRoot = Environment.getExternalStorageDirectory(); 
      //create a new file, specifying the path, and the filename 
      //which we want to save the file as. 
      File file = new File(SDCardRoot,"somefile5.zip"); 

      //this will be used to write the downloaded data into the file we created 
      FileOutputStream fileOutput = new FileOutputStream(file); 

      //this will be used in reading the data from the internet 
      InputStream inputStream = urlConnection.getInputStream(); 

      Integer downloadedSize = 0; 

      Integer totalSize = urlConnection.getContentLength(); 

      //create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; //used to store a temporary size of the buffer 

      //now, read through the input buffer and write the contents to the file 
      while ((bufferLength = inputStream.read(buffer)) > 0) { 
       //add the data in the buffer to the file in the file output stream (the file on the sd card 
       fileOutput.write(buffer, 0, bufferLength); 
       //add up the size so we know how much is downloaded 
       downloadedSize += bufferLength; 
       //this is where you would do something to report the prgress, like this maybe 

       publishProgress((downloadedSize/totalSize) * 100); 
      } 
      //close the output stream when done 
      fileOutput.close(); 

     //catch some possible errors... 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPreExecute() { 
     Intent intent = new Intent(ListActivity.this, ListActivity.class); 
     pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 

     // configure the notification 
     notification = new Notification(R.drawable.ic_stat_rom, "Downloading Rom via RomGet", System 
       .currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
     notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.layout_download); 
     notification.contentIntent = pendingIntent; 
     notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon_rom); 
     notification.contentView.setTextViewText(R.id.download_description, "Downloading"); 
     notification.contentView.setProgressBar(R.id.download_progress, 100, 0, false); 

     getApplicationContext(); 
     notificationManager = (NotificationManager) getApplicationContext().getSystemService(
       Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(43, notification); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     notification.contentView.setProgressBar(R.id.download_progress, 100, Math.round(progress[0]), false); 
     //notification.contentView.setTextViewText(R.id.download_description, Integer.toString(downloadedSize)); 
     // inform the progress bar of updates in progress 
     notificationManager.notify(43, notification); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
      notification.contentView.setTextViewText(R.id.download_description, "Done"); 
      notificationManager.notify(43, notification); 
    } 

} 

乾杯

回答

1

它正在嘗試連接的URL (phobos.emuparadise.org)顯示403禁止(嘗試使用瀏覽器)。如果你解決了這個問題,你應該很好。

+0

是的,事情是我用我的瀏覽器嘗試過......我甚至認爲可能存在與傳遞對象有關的問題,但我甚至嘗試使用直接url來代替'params [0] .url'但即使如此,沒有這樣的運氣... – jsw 2011-06-01 14:46:35

+2

不要擔心 - 我的錯誤!我忘了添加Internet權限....哈哈 – jsw 2011-06-01 14:55:59