【问题标题】:How to show ProgressDialog while waiting for HttpRespons?如何在等待 HttpRespons 时显示 ProgressDialog?
【发布时间】:2012-03-07 09:35:12
【问题描述】:

大家好,感谢阅读。 :)

我有这个 Java (Android) 代码,它发出 HTTP 请求并等待响应。该请求启动了一个生成 PDF 文件并将其返回的服务。

该服务大约需要 20 秒,当用户等待时,我想显示一个进度对话框(不确定)。我尝试在它自己的线程中显示对话框,这给了我运行时异常。我已经尝试将请求和响应放在他们自己的线程中,但是没有等待响应完成,我得到一个空的 pdf。

任何人都可以提出任何建议吗?这是代码...

textContainer.setOnClickListener(new View.OnClickListener() {

  public void onClick(View view) {

    Intent getPdfFile = null;
    File pdfFile = new File(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");

    if(!pdfFile.exists()) {

      try {

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet getMethod = new HttpGet((
                         "http://myServices.mySite.org/services.ashx/PDFFILE?fileId="
                         + fileId + "&account=" + accountId + "&dataset=" +                                                                                              
                         account.getProperty("DATASET").getValue().toString().trim()).replace(" ", "%20"));
        HttpResponse response = client.execute(getMethod);

        InputStream inStream = response.getEntity().getContent();

        FileOutputStream fileWriter = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");

        int dataByte = inStream.read();
        while(dataByte > -1) {
          fileWriter.write(dataByte);
          dataByte = inStream.read();
        }
        fileWriter.close();
      }
      catch(Exception e) {

        // TODO: Handle the exceptions...
      }

      getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(pdfFile), "application/pdf");                                    
      startActivity(getPdfIntent);
  }
});

提前非常感谢! :)

编辑:这是我使用 AsyncTask 尝试解决问题的示例。

    textContainer.setOnClickListener(new View.OnClickListener() {

      public void onClick(final View view) {

        Intent getPdfFile = null;
        File pdfFile = new File(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");

        if(!pdfFile.exists()) {

          new AsyncTask<Boolean, Boolean, Boolean>() {

              private ProgressDialog dialog;

              protected void onPreExecute() {

                  dialog = ProgressDialog.show(view.getContext(), "Loading", "Please wait...");
              }

              protected Boolean doInBackground(Boolean... unused) {

                  try {

                      DefaultHttpClient client = new DefaultHttpClient();
                      HttpGet getMethod = new HttpGet((
                                   "http://myServices.mySite.org/services.ashx/PDFFILE?fileId="
                                   + fileId + "&account=" + accountId + "&dataset=" +                                                                                                
                                   account.getProperty("DATASET").getValue().toString().trim()).replace(" ", "%20"));
                      HttpResponse response = client.execute(getMethod);

                      InputStream inStream = response.getEntity().getContent();

                      FileOutputStream fileWriter = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + fileId.trim() + ".pdf");

                      int dataByte = inStream.read();
                      while(dataByte > -1) {
                          fileWriter.write(dataByte);
                          dataByte = inStream.read();
                      }
                      fileWriter.close();
                  }
                  catch(Exception e) {

                      // TODO: Handle the exceptions...
                  }
                  return true;
              }

              protected void onPostExecute(Boolean unused) {

                  dialog.dismiss();
              }
          }.execute(0);

          getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(pdfFile), "application/pdf");                                   
          startActivity(getPdfIntent);
      }
   });

但是发生的情况是,我不等待来自 HttpRequest 的响应,而是继续,就好像响应立即返回一样。 :-/

【问题讨论】:

  • 您正在 ui 线程上进行网络调用。你不应该那样做。改用异步任务。
  • njzk2 是正确的,我认为它实际上在 Android 3.0 SDK 及更高版本上是不允许的
  • 刚刚编辑了我的帖子以包含我尝试过的 AsyncTask,但没有任何运气。
  • 谁能告诉我我做错了什么?请...这是完成第一版的最后一件事。 :)
  • @RichB 在这一点上是正确的。您需要将您的 getPdfIntent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(fakturaFil), "application/pdf"); startActivity(getPdfIntent); 代码行移动到您的 onPostExecute() 中。现在,您正在创建任务、启动它,然后立即进入下一个活动。您需要等到任务完成。

标签: android pdf-generation httprequest httpresponse progressdialog


【解决方案1】:

您似乎需要将最后两行放在 else {} 块中以及 onPostExecute 中,以便它仅在文件存在或 AsyncTask 完成后执行。

【讨论】:

  • 我爱你们,非常感谢!这解决了一切。我不知道为什么我自己没有想到,但还是非常感谢! :)
【解决方案2】:

您将需要使用 AsyncTask ...本教程应该为您提供基础知识。

http://droidapp.co.uk/?p=177

【讨论】:

  • 这就是我所做的...在 onPreExecute 方法中显示对话框,在 doInBackground 方法中发出请求并在 onPostExecute 方法中关闭对话框。发生的情况是它在响应完成之前继续尝试显示 pdf 文件。换句话说,它不会等待响应结束。当我收到无法读取文件的错误消息并按下它时,它会显示永远不会被解雇的对话框? :-|
  • 你能修改你的第一篇文章并把你正在使用的新代码放进去吗?
  • 链接死请修复
【解决方案3】:

你应该使用 AsyncTask 来做到这一点。在覆盖方法“doInBackground”上,您可以在覆盖方法“onPreExecute”上发布您的httppost和showdialog,然后在“onPostExecute”上取消它

【讨论】:

  • 这正是我尝试过的,但它不会等待响应,它会继续。
  • 我不明白你的意思是你只做一个http请求,那么你就不会关心响应。但是您的问题是在等待响应时如何 [显示] 进度对话框...什么...我想也许您可以使用处理程序来做到这一点。
猜你喜欢
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 2013-04-05
  • 1970-01-01
相关资源
最近更新 更多