0

ファイルをダウンロードできる非同期メソッドがあります。ダウンロード途中の場合は、接続(wifiまたは3g)のタイムアウトが発生しないように削除します。

常に次のループにとどまり、接続が返されるのを待ちます。

while ((count = input.read(data)) != -1) {
        System.out.println("state 5");
        total += count;
        publishProgress((int) (total * 100 / fileLength));
        output.write(data, 0, count);
}

そうです:

  private class DownloaderFile extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            ...
            try{
                URLConnection connection = urlFinal.openConnection();
                connection.setConnectTimeout(TIMEOUT_VALUE);
                connection.setReadTimeout(TIMEOUT_VALUE);
                connection.connect();
                int fileLength = connection.getContentLength();

                InputStream input = new BufferedInputStream(urlFinal.openStream());

                OutputStream output = new FileOutputStream(folder + params[0]);

                byte data[] = new byte[1024];
                long total = 0;
                int count;

                while ((count = input.read(data)) != -1) {
//always wait here
                    System.out.println("state 5");
                    total += count;
                    publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (SocketTimeoutException e) {
                System.out.println("TIMEOUT!!! " + TIMEOUT_VALUE + " elapsed.");
                callback.onDownloadEnd(DOWNLOAD_ERROR);
            }
            ...
        }
        ...
4

3 に答える 3

1

これは素晴らしい解決策ではありませんが、機能します。別の解決策を考えている間...

while ((count = input.read(data)) != -1) {
     if (!isInternet(context)){
        callback.onDownloadEnd(DOWNLOAD_ERROR);
         return "error";
     }
     total += count;
     publishProgress((int) (total * 100 / fileLength));
     output.write(data, 0, count);
}
于 2013-09-24T23:45:47.153 に答える