0

I'm trying to send POST method to server after the user click on confirm button but after click and I get the responce the ProgressDialog still runing.

paymentSubmitBtn = (Button)findViewById(R.id.paymentSubmitBtn);
paymentSubmitBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                final String receiver = receiverMymoIdEdtTxt.getText().toString();
                runOnUiThread(new Runnable() {
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
                        builder.setTitle(R.string.message);
                        builder.setMessage("Transaction Details")
                               .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) 
                                   {
                                      ProgressDialog dialog = ProgressDialog.show(Payment.this, "", "Connect to server..." , true);
                                       sendTransactionInfo(receiver);
                                   }
                               })
                               .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int which) { 
                                   }
                                });                    
                        AlertDialog alert = builder.create();
                        alert.show();               
                    }
                });

            }
        });

This is the function that I use to send the POST method and try to dismiss ProgressDialog.

protected void sendTransactionInfo(final String receiver)
    {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); 
                HttpResponse response;

                String URL = baseURL + "/api-create-transaction";

                try {
                        HttpPost post = new HttpPost(URL);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                        nameValuePairs.add(new BasicNameValuePair("section", "API" ));

                        nameValuePairs.add(new BasicNameValuePair("receiver", receiver ));          

                        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        response = client.execute(post);

                        if(response!=null)
                        {
                            String res = EntityUtils.toString(response.getEntity());

                            final JSONObject result = new JSONObject(res);

                            String operation = result.getString("operation");

                            if(operation.trim().equalsIgnoreCase("success"))
                            {                               
                                JSONObject data = result.getJSONObject("data");
                            }
                            else if(operation.trim().equalsIgnoreCase("error"))
                            {

                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
                                        builder.setTitle(R.string.message);
                                        try 
                                        {
                                            builder.setMessage(result.getString("message"))
                                                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                                       public void onClick(DialogInterface dialog, int id) {
                                                          dialog.dismiss();
                                                       }
                                                   });
                                        } catch (JSONException e) {

                                            e.printStackTrace();
                                        }                      
                                        AlertDialog alert = builder.create();
                                        alert.show();               
                                    }
                                });
                                Toast.makeText(getBaseContext(), result.getString("message") , Toast.LENGTH_LONG).show();                           
                            }
                            dialog.dismiss();
                        }

                } catch(final Exception e) {

                    runOnUiThread(new Runnable() {
                        public void run() {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
                            builder.setTitle(R.string.message);
                            builder.setMessage(e.toString())
                                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                       public void onClick(DialogInterface dialog, int id) {
                                           dialog.dismiss();
                                       }
                                   });                     
                            AlertDialog alert = builder.create();
                            alert.show();               
                        }
                    });

                }

                Looper.loop();
            }
        };
        t.start();
    }
  • 1
    i dint quite get ur question... u want to to cancel the dialog when OK in the dialog box is pressed? – Jerry Wattre May 08 '14 at 14:27
  • No i want to cancel the dialog when i get the response from server –  May 08 '14 at 14:28
  • But you are creating a new alert with on click handler. And somewhere there is a global dialog object. The dialoog declared in the on click handler ist not reachable at the end. – greenapps May 08 '14 at 17:14

2 Answers2

0

Either pass your dialog object to your sendTransactionInfo() or make ProgressDialog dialog global would be my suggestion. It looks like it may be a scope issue (just briefly looking at your code).

dan983
  • 454
  • 2
  • 5
  • 19
  • if this was scope issue then wouldn't there have been a compile time error? – Jerry Wattre May 08 '14 at 14:32
  • I'm guessing it's using the DialogInterface dialog and that's why it's not dismissing the ProgressDialog. How else can another method have access to the ProgressDialog object when it's not being passed and it's local to onClick? – dan983 May 08 '14 at 14:42
  • Problem solved when I changed the name of DialogInterface argument. –  May 11 '14 at 07:41
0

Hmmm try this for a change

alert.dismiss();

but make it global..the referene to the AlertDialog

Jerry Wattre
  • 204
  • 2
  • 8