0

Asynctask 内で複数のファイルをダウンロードする (そしてダウンロード速度をチェックする) アプリケーションがあります。しかし、3G に接続している場合 (ハンドオーバーがある場合)、またはデバイスが WiFi から 3G に切り替わると、アプリケーションがフリーズし、この問題を処理できません。

@Override
    protected Integer doInBackground(String... sUrl) {

        try {
            speed=0; //initial value 
            int i=0;
            while ((i<sUrl.length)) {



            URL url = new URL(sUrl[i]); 
            URLConnection connection = url.openConnection();
            connection.setReadTimeout(10000);
            connection.connect(); 

            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(file);

            byte data[] = new byte[1024*1024]; //1MB buffer
            long total = 0;
            int count;
            long start = System.currentTimeMillis();
            while ((count = input.read(data)) != -1) {
                total += count;

                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }
           long finish = System.currentTimeMillis();
            long tempSpeed= (fileLength *8)/(finish-start);
            if (tempSpeed>speed) {
                speed=tempSpeed;
            }


            output.flush();
            output.close();
            input.close(); // connection is closed
            i++;
            }    
        }catch(SocketTimeoutException e) {
            exceptions.add(e);
            messageProgressDialog.setCancelable(true);
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
            alertDialog.setTitle("Network problem");
            alertDialog.setMessage("connection dropped");
            alertDialog.show();
        } catch (IOException e) {
                exceptions.add(e);
messageProgressDialog.setCancelable(true);
                AlertDialog alertDialog;
                alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
                alertDialog.setTitle("IOException");
                alertDialog.setMessage("IOException error");
                alertDialog.show();
            } catch(Exception e) {
        exceptions.add(e);
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
        alertDialog.setTitle("Exception");
        alertDialog.setMessage("Exception error");
        alertDialog.show();
    } 


            return 1;
    }

stackoverflow に関する多くのトピックを読みましたが、アプリケーションの問題を解決するのに役立つものはありませんでした。try/catch 句がありますが、違いはないようです。3G を使用していて、電話機が別のアンテナに接続している場合、またはネットワークに問題がある場合、アプリケーションがフリーズします。私に何ができる ?

問題が見つかりました。InputStream input = new BufferedInputStream(url.openStream());URLの入力ストリームとして使用するのはこの行でした。私はそれを次の行に置き換えました:InputStream input = new BufferedInputStream(connection.getInputStream());タイムアウトすると、アプリケーションがクラッシュするようになりました。

}catch(SocketTimeoutException e) {
        String erro=Integer.toString(count);
        Log.d("error socketTimeout",erro);//1st log
        exceptions.add(e);
        onPostExecute(1);
        Log.d("error sockteTimeout", "here"); //2nd log to see if onPostExecute worked
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
        alertDialog.setTitle("Network problem");
        alertDialog.setMessage("connection dropped");
        alertDialog.show();

私が使っているキャッチです。catch 句内で onPostExecute メソッドを呼び出そうとすると、アプリケーションがクラッシュするようです。

protected void onPostExecute(Integer result) {
    Log.d("onpost was called", "here");
messageProgressDialog.setMessage("Your speed is: " + speed +" KBit/sec");
        messageProgressDialog.setCancelable(true);

}

ログ ビューアを見ると、1 つ目のログしか表示されません。onPostExecute が呼び出されると、アプリがクラッシュします。onPostExecute が実際に実行されることはありません。何か案は?

4

1 に答える 1

2

問題は、 の戻り値の処理に関連している可能性がありますinput.read()。ソケットが閉じている場合、input.read()0 を返すことがあります (他のエラー状況では -1 を返すこともあります)。0 が返されると、ループがハングします。私は次のようなものを提案します:

        while ((count = input.read(data)) > 0) {

そうすれば、ダウンロードの進行中にループが実行されます。

于 2013-06-04T01:06:51.140 に答える