3

私は asynctask を使用してファイルをダウンロードしています。Androidのwifi接続をオフにするまで(他のインターネット接続はありません)、ダイアログをダウンロードして変更を加えるまで、正常に動作します。ログで確認するとinputstreamの関数read()がノンストップであることがわかりました。では、このケースを確認するにはどうすればよいでしょうか。ここに私のコードがあります:

URL url = new URL(this.url);
        URLConnection connection = url.openConnection();
        connection.setReadTimeout(1000);
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();
        fileName = "temp.zip";

        // download the file
        InputStream input = new BufferedInputStream(connection.getInputStream());
        OutputStream output = new FileOutputStream(path+fileName);

        byte buffer[] = new byte[1024000];
        long total = 0;
        int count;
        Log.v("test download:","download in background");
        while (((count = input.read(buffer)) != -1)) {
            Log.v("test download:","read:"+count);
            total += count;
            publishProgress((int) (total * 100 / fileLength - 1));
            output.write(buffer, 0, count);
        }
4

2 に答える 2

8

を呼び出してタイムアウトを設定しているので、接続が切断された直後にsetReadTimeout()取得する必要があります。SocketTimeoutException

あなたのコードはたまたまこの例外を黙ってキャプチャしますか?

于 2012-08-22T21:33:10.460 に答える
0

byte buffer[] = new byte[1024];ファイルのダウンロード バッファ サイズが大きすぎます

これを試してみると、Androidでファイルをダウンロードし、 asynctaskを使用してProgressDialog(トップアンサー)で進行状況を表示すると、正常に動作し、その問題は発生しませんでした

于 2012-08-23T07:15:38.703 に答える