【发布时间】:2014-04-22 12:58:36
【问题描述】:
我已经制作了一个异步任务,用于从 Web 服务获取一些 Json 响应。我在后台处理完成时使用了一个进度条,并在我的 asynctask 的 onPostExecute 方法中完成了进度对话框。事情是我得到了成功的响应根据需要,但之后 myl 进度对话框仍然可见,请任何人告诉我如何关闭它。我的代码如下: main.java
私有类 DoFavourite 扩展 AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ProductDetailActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
String favUrl = Const.API_DO_FAVOURITE + "?product_id=" + pid + "&customer_id=" + Pref.getValue(ProductDetailActivity.this, Const.PREF_CUSTOMER_ID, "");
System.out.println(":::::::::my FAVOURITE URL::::::::::::::" + favUrl);
// Creating service handler class instance
BackendAPIService sh = new BackendAPIService();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(favUrl, BackendAPIService.GET);
Log.d("Response: ", "> " + jsonStr);
System.out.println("=============MY RESPONSE==========" + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
if (jsonObj.has(Const.TAG_STATUS)) {
if (jsonObj.getString(Const.TAG_STATUS).equalsIgnoreCase("success")) {
if (jsonObj.getString(Const.TAG_FAVOURITE).equalsIgnoreCase("1")) {
flag = 1;
} else {
flag = 2;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
【问题讨论】:
-
请登录 onPostExecute(Void result) 方法并检查您的情况
-
条件有什么问题?一切都好吗?
-
替换 if (pDialog.isShowing()) pDialog.dismiss();只需 pDialog.dismiss()
标签: android android-asynctask progressdialog