0

这是一个有点抽象的问题,但想知道是否有任何人可能对我有任何简单的建议或答案。

我有一个 AsyncTask,其中包含一个用于下载几个文本文件的循环,这些文件很小,大约有 12 个。目前下载最多需要 10 秒,这比我想象的要长,有什么我可以做的可以加快下载速度,或者有没有办法同时下载它们而不是一个一个?

这是我用于异步和下载的代码。

@Override
protected Long doInBackground(URL... params) {



    try { 
        // Loop to download 
        distance dis = new distance();
        URL[] urlArray = dis.urlArray();
        URL[] urlExtra = dis.urlArrayExtra();
        String [] stationName = dis.getStationName();
        String [] stationNameExtra = dis.getStationNameExtra();
        for  (int i = 0; i < urlArray.length; i++) {

        String fileName = stationName[i];

        File file = new File(sdCard.getAbsolutePath() +
                "/Download/" + fileName);
        URL url = urlArray[i];
        /* 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();
        }
} catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
}
    return null;

}
4

1 回答 1

0

我认为瓶颈可能是您的 ByteArrayBuffer 仅设置为 50。

这是我用来下载一些文件的代码。

        @Override
    protected String doInBackground(Void... params) {
        String filename = "inputAFileName";

        HttpURLConnection c;
        try {
            URL url = new URL("http://someurl/" + filename);
            c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
        } catch (IOException e1) {
            return e1.getMessage();
        }

        File myFilesDir = new File(Environment
                .getExternalStorageDirectory().getAbsolutePath()
                + "/Download");

        File file = new File(myFilesDir, filename);

        if (file.exists()) {
            file.delete();
        }

        if ((myFilesDir.mkdirs() || myFilesDir.isDirectory())) {
            try {
                InputStream is = c.getInputStream();
                FileOutputStream fos = new FileOutputStream(myFilesDir
                        + "/" + filename);

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

            } catch (Exception e) {
                return e.getMessage();
            }

            if (file.exists()) {
                return "File downloaded!";
            } else {
                 Log.e(TAG, "file not found");
            }
        } else {
            Log.e(TAG, "unable to create folder");
        }
    }

您可以尝试一下,看看它是否更快。

于 2013-11-06T23:59:57.683 回答