我使用 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;
}
}
现在我怎么能阻止它并显示吐司???
提前致谢