Hessi-Dude
Hessi-Dude

Reputation: 61

How to show ProgressDialog while waiting for HttpRespons?

Hi all and thanks for reading. :)

I have this Java (Android) code which is making a HTTP request and waiting for a respons. The request starts a service that generates a PDF-file and returns it.

The service takes about 20 seconds, and while the user is waiting I want to show a progress dialog (indefinate). I've tried showing the dialog in it's own thread which gives me runtime exceptions. I've tried putting the request and respons in their own thread but then there's no waiting for the respons to complete and i get an empty pdf.

Can anyone suggest anything? here's the code...

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);
  }
});

Big thanks in advance! :)

EDIT: Here's an example where I've used AsyncTask to try and solve the problem.

    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);
      }
   });

But what happens is that i doesn't wait for the response from the HttpRequest and continues as if the response was returned immidiately. :-/

Upvotes: 0

Views: 2746

Answers (3)

Rich B
Rich B

Reputation: 121

It looks like you need to put your last two lines inside of an else {} block and also in the onPostExecuteso that it only executes if the file exists or after the AsyncTask has completed.

Upvotes: 2

sam
sam

Reputation: 1

u should use AsyncTask to do this. on the override method "doInBackground" u can post your httppost and showdialog on override method "onPreExecute" ,then cancel it on "onPostExecute"

Upvotes: 0

Tony
Tony

Reputation: 2425

You will need to use an AsyncTask ... this tutorial should give you the basics.

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

Upvotes: 0

Related Questions