在我的代码中,我使用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);
}
}
答案 0 :(得分:0)
替换pDialog.dismiss();
与
pDialog.cancel();
答案 1 :(得分:0)
int success;//Globally declared
在onPostExecute中执行:
if (success == 1) {
} else {
Intent i = new Intent(getApplicationContext(),
NewCategory.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
和
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);
内部for循环创建问题
答案 2 :(得分:0)
确保ProgressDialog的上下文是调用活动。那是 : pDialog = new ProgressDialog(AllCategories.this);
AllCategories应该是启动任务的活动。希望这有帮助