Android http错误

Android http错误,android,http,Android,Http,我正试图从Dropbox下载一个文件,但我的应用程序chrash和错误日志显示http错误,我做错了什么?错误日志只是说“由以下原因引起:android.os.NetworkOnMainThreadException”,然后列出了许多http错误 private final String PATH = "/data/data/com.emiliogaines.fuelfinder/shared_prefs/"; //put the downloaded file here publ

我正试图从Dropbox下载一个文件,但我的应用程序chrash和错误日志显示http错误,我做错了什么?错误日志只是说“由以下原因引起:android.os.NetworkOnMainThreadException”,然后列出了许多http错误

private final String PATH = "/data/data/com.emiliogaines.fuelfinder/shared_prefs/";  //put the downloaded file here


    public void DownloadFromUrl(String fileName) {  //this is the downloader method
        try {

            URL url = new URL("https://www.dropbox.com/s/1793397jyxorw6g/testDoc.xml?dl=0");
            File file = new File(PATH + fileName);

            long startTime = System.currentTimeMillis();
            Log.d("ImageManager", "download begining");
            Log.d("ImageManager", "download url:" + url);
            Log.d("ImageManager", "downloaded file name:" + fileName);
                    /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

                    /*
                     * Define InputStreams to read from the URLConnection.
                     */
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

                    /*
                     * Read bytes to the Buffer until there is nothing more to read(-1).
                     */
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

                    /* Convert the Bytes read to a String. */
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            Log.d("ImageManager", "download ready in"
                    + ((System.currentTimeMillis() - startTime) / 1000)
                    + " sec");

        } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
        }

    }

NetworkOnMainThreadException表示您试图在主线程上访问网络,尝试创建一个新线程并在那里访问网络

当应用程序试图在其主线程上执行网络操作时引发的异常。()


好的,谢谢!但是我真的不懂线程,你能编辑一下代码吗?我从来没有让他们工作尝试这个链接,在那里有如何在android中做线程的例子。类似于
new Thread(new Runnable(){public void run(){…}})
然后将上面的代码包含在
run()
方法中