在我的代码中,我正在使用json代码从在线MySQL数据库获取数据。
问题是进度对话框在某些情况下永远不会完成,只是不断循环而不获取任何数据!并非总是如此,在大多数情况下,它就像一种魅力。
这是代码,任何想法!!!
class LoadAllProducts extends AsyncTask<String, String, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllCategories.this);
pDialog.setMessage("loading all categories");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args)
{
String url_all_categories = "http://www.*****.com/****/****.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.makeHttpRequest(url_all_categories,
"GET", params);
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
categories = json.getJSONArray(TAG_CATEGORIES);
for (int i = 0; i < categories.length(); i++) {
JSONObject c = categories.getJSONObject(i);
id = c.getString(TAG_CID);
name = c.getString(TAG_NAME);
descAR = c.getString(TAG_DESC_AR);
picture = c.getString(TAG_PICTURE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_CID, id);
map.put(TAG_NAME, name);
map.put(TAG_DESC_AR, descAR);
map.put(TAG_PICTURE, picture);
categoriesList.add(map);
}
} else {
Intent i = new Intent(getApplicationContext(),
NewCategory.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
adapter = new LazyAdapter(AllCategories.this, categoriesList);
setListAdapter(adapter);
}
}
最佳答案
替换pDialog.dismiss();
与
pDialog.cancel();
关于android - AsyncTask,doinBackground永远不会完成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23472526/