0

我使用 asynctask 从远程 url 获取 json 数据,但有时这个 url 在 json 数据中返回错误,如下所示:

{
   "error": {
      "message": "(#803) Some of the aliases you requested do not exist: RNN.NES",
      "type": "OAuthException",
      "code": 803
   }
}

在doInBackground中,我检查json是否有错误..

if(true) {
    //cancel asynctask and show toast with message i use 
    cancel(true)
}

但我认为条件是真实的,但它没有被取消

这是我的代码:

private class MyAsyncTask extends AsyncTask<String, Void, Void> {

        ProgressDialog mProgressDialog;

        @Override
        protected void onPostExecute(Void result) {
            db = new DAOPages(context);
            db.open();
            db.addPage(name, fid, picName);
            getPagePrefs(fid);
            db.close();
            mProgressDialog.dismiss();

            Intent intent = new Intent(context, PagePrefsActivity.class);
            intent.putExtra("fPageid", fid);
            context.startActivity(intent);

        }

        @Override
        protected void onPreExecute() {

            mProgressDialog = ProgressDialog.show(context, "Loading...",
                    "Data is Loading...");
        }

        @Override
        protected Void doInBackground(String... params) {

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(params[0]);
            // addPageData(params[0]);
            try {

                if(json.getJSONObject("error") != null) {
                    Log.e("error", "true");
                    cancel(true);
                }
                name = json.getString("name");
                fid = json.getString("id");                                 

                String picture = json.getJSONObject("picture")
                        .getJSONObject("data").getString("url");
                picName = downloadImage(picture, fid);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

现在我怎么能阻止它并显示吐司???

提前致谢

4

2 回答 2

1

cancel(true)方法不会停止 AsyncTask。它只是在 doInBackGround 返回之后调用onCancelled()而不是调用。onPostExecute()您可以使用AsyncTask.isCancelled(),如果您取消 AsyncTask,它将返回 true,以防止在 doInBackground() 中进行进一步处理。

所以覆盖 onCancelled() 并将你的 Toast 放在那里。

private class MyAsyncTask extends AsyncTask<String, Void, Void> {

        ProgressDialog mProgressDialog;

        @Override
        protected void onPostExecute(Void result) {
            db = new DAOPages(context);
            db.open();
            db.addPage(name, fid, picName);
            getPagePrefs(fid);
            db.close();
            mProgressDialog.dismiss();

            Intent intent = new Intent(context, PagePrefsActivity.class);
            intent.putExtra("fPageid", fid);
            context.startActivity(intent);

        }

        @Override
        protected void onPreExecute() {

            mProgressDialog = ProgressDialog.show(context, "Loading...",
                    "Data is Loading...");
        }

        @Override
        protected Void doInBackground(String... params) {

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(params[0]);
            // addPageData(params[0]);
            try {

                if(json.getJSONObject("error") != null) {
                    Log.e("error", "true");
                    cancel(true); // after this AsyncTask.isCancelled() returns true
                }

                if(!this.isCancelled()){ // if it's true, prevent further processing
                     name = json.getString("name");
                     fid = json.getString("id");                                 

                     String picture = json.getJSONObject("picture")
                            .getJSONObject("data").getString("url");
                     picName = downloadImage(picture, fid);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        // will be called instead onPostExecute after a cancel
        @Override
        protected void onCancelled() {
            // Show your Toast 
        }
    }
于 2013-09-18T09:27:55.180 回答
0

您可以在 onPostExecute() 方法中使用 isCancelled() 进行检查,并可以在该方法中显示 Toast。您可以这样做 - :

    @Override
    protected void onPostExecute(Void result) {
        if(isCancelled()) {
            Toast.makeText(context, "your text", Toast.LENGTH_SHORT).show();
            return;
        }
        db = new DAOPages(context);
        db.open();
        db.addPage(name, fid, picName);
        getPagePrefs(fid);
        db.close();
        mProgressDialog.dismiss();

        Intent intent = new Intent(context, PagePrefsActivity.class);
        intent.putExtra("fPageid", fid);
        context.startActivity(intent);

    }
于 2013-09-18T09:26:17.363 回答