-1

下面是我创建的一个异步类,我试图在执行时实现一个对话框,并在完成时实现一个 Toast。

怎么从来没有吐司或对话出现过。

我的 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();
            }
        }

    }

}

我如何从我的活动中调用它:

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

我的日志 在此处输入图像描述

4

3 回答 3

1

您不应尝试从doInBackground(). AsyncTasks 的目的doInBackground()是避免陷入 UI 线程......相反,您应该以适当的方法执行 UI 工作:onPreExecute()、、、onProgressUpdate()onPostExecute()

于 2012-12-13T19:22:26.587 回答
1

我怀疑吐司没有显示,因为您的结果始终为空。您的日志在帖子中显示错误

正如其他人所说,从 onPreExecute() 开始您的进度对话框

于 2012-12-13T21:53:49.617 回答
0

我注意到您在 doInBackground() 中实例化了您的 progressDialog。将其移至 onPreExecute() 。doInBackground() 只应该做非 UI 工作。=)

这应该“解决”您的问题。

于 2012-12-13T19:32:28.487 回答