4

Web サイトからファイルをダウンロードして表示する単純な asynctask を開発しました。
奇妙な動作があります:

ワイヤレス接続を使用する場合

ProgressDialog は、ダウンロードの開始時に正しく表示されます。ダウンロードが終了すると
、ProgressDialog は 0% から 100%
に正しく更新され、消えて PDF ファイルが開かれます。

3G/モバイル接続を利用する場合

ProgressDialog は、ダウンロードの開始時に正しく表示されます。ダウンロードが終了すると
、ProgressDialog は 0%
のままになり、消えて PDF ファイルが正しく開かれます。

何が問題なのですか?(私は Android 2.3.7 を使用しています)
ありがとうございます!

public class TamTam extends Activity {
    public ProgressDialog mProgressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mProgressDialog = new ProgressDialog(TamTam.this);
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        new DownloadFileAsync().execute("http://www.*******.com/file.pdf");
    }

    class DownloadFileAsync extends AsyncTask<String, Integer, String> {

        String fileName;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog.show();
        }

        @Override
        protected String doInBackground(String... aurl) {
            int count;

            try {
                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();

                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();

                fileName="output.pdf";
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(new File(
                        Environment.getExternalStorageDirectory()
                            + "/" + fileName));

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

                while ((count = input.read(data)) > 0) {
                    total += count;
                    publishProgress((int) ((total * 100) / lenghtOfFile));
                output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {
            }
            return null;

        }

        protected void onProgressUpdate(Integer... progress) {
            mProgressDialog.setProgress(progress[0]);
        }

        @Override
        protected void onPostExecute(String unused) {
            mProgressDialog.dismiss();
                    showPdf(fileName);
        }
    }
    public void showPdf(String fileName)
    {
       ....
    }
}
4

1 に答える 1

1

マイクが言ったように、問題は次の行にありました。

int lenghtOfFile = conexion.getContentLength();

問題は、サーバーから content-length ヘッダーが提供されていなかったことです。HEAD リクエストを使用して、それ以外の場合は空だった正しい content-length フィールドをサーバーに返信させました。リクエスト メソッドを指定するには、URLConnection の代わりに HttpURLConnection を使用する必要がありました。

だから私はこのブロックを変更しました

URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();

これとともに

HttpURLConnection conexion2 = (HttpURLConnection) url.openConnection();
conexion2.setRequestMethod("HEAD");
int lenghtOfFile = conexion2.getContentLength();

URLConnection conexion = url.openConnection();
conexion.setDoOutput(true);
conexion.connect();

ソース:

http://en.wikipedia.org/wiki/Chunked_transfer_encoding

Java URLConnection : Web ファイルのサイズを調べるにはどうすればよいですか?

于 2013-08-11T09:59:33.893 に答える