-1

Below is an async class i created that i am trying to implement a dialog on execute and a Toast on complete.

How ever no toast or dialog are ever showing up.

my asykTask:

public class EmailPictureService extends HTTPRequestAsyncTask {
    Context context;
    ProgressDialog dialog;
    public EmailPictureService(Context context){
        this.context = context;
        //dialog = new ProgressDialog(context);
    }
    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }   

    @Override
    protected String doInBackground(Object... params) {
        Log.v("Start EMAIL SERVICE","START YOPPPPPPPPPPPPPPPPPPPPPP!");
        dialog = new ProgressDialog(context);
        dialog.setMessage("Sending...");
        dialog.setIndeterminate(true);
        dialog.show();
        HTTPInvoker invoker = new HTTPInvoker();
        HttpResponse response = null;

        EmailPicture emailPicture = new EmailPicture();
        emailPicture.setDeviceType("TABLET");
        emailPicture.setStoreId((String)params[1]);
        emailPicture.setNotificationType("E");
        emailPicture.setRecipientName((String)params[2]);
        emailPicture.setRecipientEmail((String)params[3]);

        String jsonString = JSONConverter.toJson(emailPicture);
        response = invoker.invokePOSTFileService((String)params[0], jsonString, (File[])params[4]);
        return parseHttpResponse(response);
    }

    @Override
    protected void onPostExecute(String result) {
        String msg = "";
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (result != null) {
            JSONObject jsonObject = null;
            long errorCode = 0;
            try {
                jsonObject = new JSONObject((String) result);
                errorCode = jsonObject.getLong("errorCode");
                if(errorCode<1){
                    msg ="Success, your picture has been sent";
                }else{
                    msg = "Sorry, there was an error sending your picture. Please try again later.";
                }
                Log.i(Constants.TAG, "Error Code...." + errorCode);
                Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
                toast.show();

            } catch (JSONException e1) {
                Log.i(Constants.TAG, "Exception...." + e1);
                Toast toast = Toast.makeText(context, "Failure: "+e1, Toast.LENGTH_SHORT);
                toast.show();
                e1.printStackTrace();
            }
        }

    }

}

how i call it from my activity:

new EmailPictureService(this).execute(url,storeID,cusName, cusEmail, new File[]{file});

my log enter image description here

erik
  • 4,946
  • 13
  • 70
  • 120

3 Answers3

1

You should not attempt to access the UI from doInBackground(). The purpose of AsyncTasks and doInBackground() is avoid bogging down the UI thread... Instead you should preform the UI work in the appropriate methods: onPreExecute(), onProgressUpdate(), onPostExecute(), etc

Sam
  • 86,580
  • 20
  • 181
  • 179
1

I suspect the toast isn't showing because your result is always null. Your log shows an error on the post

As others have said, start your progress dialog from onPreExecute()

Philip Pearl
  • 1,523
  • 16
  • 26
0

I note that you instantiate your progressDialog in doInBackground(). Move it to onPreExecute() instead. doInBackground() are only supposed to do non-UI work. =)

This should "solve" your problem.

Widerberg
  • 1,118
  • 1
  • 10
  • 24