Android ProgressDialog在移动网络下无法工作

4
我已经开发了一个简单的异步任务,从网站下载文件并显示它。
这里有一个奇怪的行为:

当我使用无线连接时

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)
    {
       ....
    }
}

1
代码提示:如果你在发布进度方面遇到了困难,可以让你的AsyncTask使用‘整数(Integer)’来更新进度,这样就可以避免字符串转换/整数解析。此外,为了使你的代码更清晰,建议将‘onPostExecute()`方法中的参数类型从‘String'改为‘Void'(大写V)。 - A--C
1
你验证过在3G情况下"lengthOfFile"是否有意义了吗? - Henry Hu
2
几乎可以确定这与ProgressDialog类本身无关,而是与不同的网络行为有关,包括如何测量进度。我会关注从3G网络返回的状态/进度信息类型。 - Mike Repass
非常感谢!你是对的:问题出在lengthOfFile上。我已经解决了,像这样:HttpURLConnection conexion = (HttpURLConnection) url.openConnection(); conexion.setRequestMethod("HEAD"); 以找到文件的长度。 - Davide Sanvito
如果您已经找到了解决方案,请发布您的答案并“接受”它。 - grattmandu03
1个回答

1
正如Mike所说,问题出在这一行:

int lenghtOfFile = conexion.getContentLength();

问题在于服务器没有提供content-length头。 使用HEAD请求,我让服务器回复正确的content-length字段,否则该字段为空。为了指定请求方法,我必须使用HttpURLConnection,而不是URLConnection。
所以我已经更改了这个块。
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文件的大小?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接