Android 异步任务,doinBackground永远不会完成
在我的代码中,我使用json代码从在线MySQL数据库获取数据。 问题是,在某些情况下,“进度”对话框永远不会结束,只是在不获取任何数据的情况下不断循环!并不总是这样,在大多数情况下,它就像一个符咒 这是代码,任何想法Android 异步任务,doinBackground永远不会完成,android,mysql,android-asynctask,Android,Mysql,Android Asynctask,在我的代码中,我使用json代码从在线MySQL数据库获取数据。 问题是,在某些情况下,“进度”对话框永远不会结束,只是在不获取任何数据的情况下不断循环!并不总是这样,在大多数情况下,它就像一个符咒 这是代码,任何想法 class LoadAllProducts extends AsyncTask<String, String, String> { ProgressDialog pDialog; @Override protected void onPreE
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.discover
与
在onPostExecute内执行:
if (success == 1) {
} else {
Intent i = new Intent(getApplicationContext(),
NewCategory.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
及
内部for循环创建问题请确保ProgressDialog的上下文是调用活动。即: pDialog=新建ProgressDialogAllCategories.this
AllCategories,应该是启动任务的活动。希望这能帮上忙,但我认为问题出在doinbackground,我认为它没有达到onPostExecute方法?将日志放入各个临界点会让您知道问题所在。也不要从后台线程启动活动
int success;//Globally declared
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);