-1

以下は、実行時にダイアログを実装し、完了時にトーストを実装しようとしている、私が作成した非同期クラスです。

トーストやダイアログが表示されることはありません。

私の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

からUIにアクセスしようとしないでくださいdoInBackground()。AsyncTasksの目的はdoInBackground()、UIスレッドの停止を回避することです...代わりに、適切なメソッドでUI作業を実行する必要があります:onPreExecute()、、、onProgressUpdate()などonPostExecute()

于 2012-12-13T19:22:26.587 に答える
1

結果が常にnullであるため、トーストが表示されていない可能性があります。ログに投稿のエラーが表示されます

他の人が言っているように、onPreExecute()から進行状況ダイアログを開始します

于 2012-12-13T21:53:49.617 に答える
0

doInBackground()でprogressDialogをインスタンス化することに注意してください。代わりにonPreExecute()に移動してください。doInBackground()は、UI以外の作業のみを行うことになっています。=)

これで問題が「解決」するはずです。

于 2012-12-13T19:32:28.487 に答える