2

I am trying to get data from a webserver and display a message saying "ok" or "invalid key". While it is getting the data it should produce a progress dialog. I have tried many methods including putting it inside of a thread however inside a thread the alert dialogs won't work. I have tried to use the async task method but it doesnt seem to work either. Can someone help me find a solution? Thanks

verifyCode.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
    //I surround the async task with the progressdialog so that it should show while the method is working in the background
        final ProgressDialog progressDialog = ProgressDialog.show(
                    Activate.this, "", "Loading...");
            new checkActivationCode().execute(activationCode.toString().toUpperCase()
                    );

            progressDialog.dismiss();       
        }
    });

   public class checkActivationCode extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... activationCode) {
        // TODO Auto-generated method stub
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpost = new HttpPost(
                    "https://iphone-radar.com/accounts/confirmation");

            JSONObject holder = new JSONObject();

            holder.put("code", activationCode);
            StringEntity se = new StringEntity(holder.toString());
            httpost.setEntity(se);
            httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json");

            ResponseHandler responseHandler = new BasicResponseHandler();
            String response = httpclient.execute(httpost, responseHandler);

            if (response != null) {
                org.json.JSONObject obj = new org.json.JSONObject(response);

                if ("00000000-0000-0000-0000-000000000000".equals(obj
                        .getString("id"))) {
                    new AlertDialog.Builder(Activate.this)
                            .setTitle(
                                    getResources().getString(
                                            R.string.InvalidKey))
                            .setMessage(
                                    getResources()
                                            .getString(
                                                    R.string.PleaseEntervalidRegistration))
                            .setNeutralButton("OK", null).show();

                } else {
                    // add permanent variables
                    SharedPreferences prefs = getSharedPreferences(
                            "Settings", 0);
                    SharedPreferences.Editor editor = prefs.edit();

                    editor.putBoolean("ACTIVATED", true);
                    editor.putString("ID", obj.getString("id"));
                    editor.commit();

                    Intent imTracking = new Intent(Activate.this,
                            ImTracking.class);
                    imTracking.putExtra("ActivationSuccessful", true);
                    // transfer more data
                    startActivity(imTracking);
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return null;
    }

}
Sean
  • 1,123
  • 4
  • 24
  • 44

2 Answers2

3

You can't show any kind of dialog or progress dialog from the doInBackground() method because it doesn't run on the UI Thread. You will want to update the UI with progress updates using the onProgressUpdate() method. If you want to show an AlertDialog on completion, use the onPostExecute() method. Both onProgressUpdate() and onPostExecute() run on the UI thread, so your AlertDialog code will work properly.

Here's the AsyncTask page with some sample code on how to properly use it (including where to provide updates to the UI): http://developer.android.com/reference/android/os/AsyncTask.html

Here's some SO questions covering the same topic: How to use AsyncTask to show a ProgressDialog while doing background work in Android?

Updating progress dialog in Activity from AsyncTask

How can I remove an alert dialog after a AsyncTask has done it's work

Community
  • 1
  • 1
pav
  • 142
  • 1
  • 10
  • 1
    With the caveat that you need to call `publishProgress()` from the `doInBackground` methods which in turn calls your `onProgressUpdate()` – citizen conn Aug 15 '11 at 23:20
  • my method needs to check what the response is, how do I pass variables from the doinBackground method to the onPostExecute method? thanks – Sean Aug 15 '11 at 23:23
  • Whatever you return from doInProgress will be the parameter "result" that is passed to the onPostExecute. You can specify what that parameter type is by specifying the 3rd generic type argument in AsyncTask @CitizenConn Yes, you're correct. Thanks. – pav Aug 15 '11 at 23:29
0

Out of curiosity...what happens if you remove the "dismiss" call immediatelty underneath your line where you show the dialogue? Any chance the dialogue is getting dismissed immediately upon creation?

Also...with other dialogues you have to go

dialogue.show()

Is a progress dialogue any different? From the docs....

Opening a progress dialog can be as simple as calling ProgressDialog.show(). For example, the progress dialog shown to the right can be easily achieved without managing the dialog through the onCreateDialog(int) callback, as shown here:

Delete
  • 922
  • 8
  • 12
  • The progress dialog shows when a thread is right underneath it and my alert dialog works when there is no thread outside of it. I want to be able to show both the progress dialog and then an alert dialog if something happens. My .show() is inside the async class – Sean Aug 15 '11 at 22:33