0

大家好,感谢您的阅读。 :)如何在等待HttpRespons时显示ProgressDialog?

我有这个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的响应,并继续作为如果回应immidiately返回。 : -/

+0

您正在ui线程上进行网络调用。你不应该那样做。改为使用异步任务。 – njzk2 2012-03-07 09:38:00

+0

njzk2是正确的,我认为它实际上是不允许在Android 3.0 SDK和以上 – Tony 2012-03-07 09:40:30

+0

刚刚编辑我的帖子,包括我试过的AsyncTask,没有任何运气。 – 2012-03-07 14:37:09

回答

2

它看起来像你需要把你的最后两行内else {}块,也在onPostExecute中,以便它只在文件存在或AsyncTask完成后才执行。

+0

我爱你们,非常感谢!这固定了一切。我不知道为什么我自己没有想到,但非常感谢! :) – 2012-03-09 09:37:35

0

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

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

+0

+1是的同意此链接。检查链接中给出的代码。 – 2012-03-07 09:46:22

+0

以下是我所做的...在onPreExecute方法中显示对话框,在doInBackground方法中发出请求并在onPostExecute方法中关闭对话框。会发生什么是它在响应完成之前继续尝试并显示PDF文件。换句话说,它不会等待响应结束。当我收到错误消息,指出文件无法读取并按回时显示永远不会被解除的对话框? : - | – 2012-03-07 10:34:49

+0

你可以修改你的第一篇文章,并把你正在使用的新代码? – Tony 2012-03-07 10:54:40

0

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

+0

这正是我已经尝试过,但它不会等待响应,它只是继续。 – 2012-03-07 10:00:28

+0

我不明白,你的意思是你只做一个HTTP请求,那么你就不会在乎响应。但你的问题是如何在等待回应时显示progressdialog ...什么都可以。我想也许你可以使用hanlder来做到这一点。 – sam 2012-03-22 06:29:32