未找到主机错误AsyncTask

时间:2011-06-01 14:27:43

标签: android

我有以下代码在通知系统中下载文件。但它似乎没有工作......我不确定为什么,一切看起来都很好。调试器给我一个未知的主机错误?

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 个答案:

答案 0 :(得分:1)

它尝试连接的网址(phobos.emuparadise.org)显示403 Forbidden(尝试使用浏览器)。如果你解决了这个问题,你应该做得很好。