我正在开发使我能够从 php mysql 加载用户的应用程序,当我选择其中一个时,它会打开一个新活动以显示用户名和删除它的按钮。问题是当我运行应用程序时,AllUsersActivity 不会返回用户。它显示一个空白页面,这里是方法 LoadAllUsers 的代码,如果没有任何用户,则有一个“else”移动到另一个 avtivity 以创建用户。但我删除了它,因为我希望它显示用户并在不添加的情况下删除它们。在我删除添加部分后,它返回给我 null,尽管连接工作正常并且它从 logcat 中的数据库中检索数据。任何人都可以帮我吗
/**
* Background Async Task to Load all users by making HTTP Request
* */
class LoadAllUsers extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllUsersActivity.this);
pDialog.setMessage("Loading users. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All users from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_users, "GET", params);
// Check your log cat for JSON reponse
Log.d("All users: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// user found
// Getting Array of user
user = json.getJSONArray(TAG_USER);
// looping through All users
for (int i = 0; i < user.length(); i++) {
JSONObject c = user.getJSONObject(i);
// Storing each json item in variable
String U_mail = c.getString(TAG_UMAIL);
String Name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_UMAIL, U_mail);
map.put(TAG_NAME, Name);
// adding HashList to ArrayList
usersList.add(map);
}
}
}
catch (JSONException e) {
e.printStackTrace(); }
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all users
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllUsersActivity.this, usersList,
R.layout.list_item, new String[] { TAG_UMAIL,
TAG_NAME},
new int[] { R.id.um, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
这是日志猫
04-23 09:22:36.814: W/System.err(916): org.json.JSONException: No value for users
04-23 09:22:36.814: W/System.err(916): at org.json.JSONObject.get(JSONObject.java:354)
04-23 09:22:36.814: W/System.err(916): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
04-23 09:22:36.824: W/System.err(916): at com.example.androidhive.AllUsersActivity$LoadAllUsers.doInBackground(AllUsersActivity.java:140)
04-23 09:22:36.824: W/System.err(916): at com.example.androidhive.AllUsersActivity$LoadAllUsers.doInBackground(AllUsersActivity.java:1)
04-23 09:22:36.824: W/System.err(916): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-23 09:22:36.865: W/System.err(916): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-23 09:22:36.924: W/System.err(916): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-23 09:22:36.924: W/System.err(916): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:573)