【问题标题】:Android ProgressDialog doesn't work with mobile networkAndroid ProgressDialog 不适用于移动网络
【发布时间】:2013-01-28 23:31:10
【问题描述】:

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

当我使用无线连接时

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

【问题讨论】:

  • 代码提示:发布进度让你自己很难受。让您的 AsyncTask 使用 Integer 进行进度更新,这样您就可以避免 String 转换/Integer 解析。此外,为了使您的代码更清晰,请让 onPostExecute() 使用 Void(大写 V)而不是 String
  • 您是否验证过“lengthOfFile”在 3G 案例中有意义?
  • 几乎可以肯定这与 ProgressDialog 类本身无关,而是与您是否/如何衡量进度的不同网络行为有关。我将重点关注您从 3G 网络返回的状态/进度信息的类型。
  • 非常感谢!你是对的:问题出在 lengthOfFile 上。我已经这样解决了: HttpURLConnection conexion = (HttpURLConnection) url.openConnection(); conexion.setRequestMethod("HEAD");查找文件的长度
  • 如果您找到了解决方案,请发布您的答案并“接受”它。

标签: android android-asynctask progressdialog


【解决方案1】:

正如迈克所说,问题出在这一行:

int lenghtOfFile = conexion.getContentLength();

问题是服务器没有提供 content-length 标头。 使用 HEAD 请求,我让服务器回复正确的内容长度字段,否则该字段为空。要指定请求方法,我必须使用 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 : how can I find out the size of a web file?

【讨论】:

    猜你喜欢
    • 2023-02-13
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2021-07-04
    • 2014-02-18
    • 2019-05-16
    相关资源
    最近更新 更多