目前,我正在尝试开发使用PHP和Android Studio的项目。我正在使用列表视图。我已经用过谷歌其他教程,但没有找到任何答案,但是在使用列表视图时仍然有问题。
05-30 23:10:15.592 7820-7849/com.example.ayim.madoc D/All Products:﹕ {"success":1,"patient":[{"id":"1","name":"Ikhwan Johari"},{"id":"2","name":"Ruminah Bedah"},{"id":"3","name":"Kamal Sado"}]}
05-30 23:10:15.592 7820-7849/com.example.ayim.madoc W/System.err﹕ org.json.JSONException: No value for idPatient
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:354)
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at org.json.JSONObject.getString(JSONObject.java:510)
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at com.example.ayim.madoc.ListPatient$LoadAllPatient.doInBackground(ListPatient.java:112)
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at com.example.ayim.madoc.ListPatient$LoadAllPatient.doInBackground(ListPatient.java:69)
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-30 23:10:15.593 7820-7849/com.example.ayim.madoc W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-30 23:10:15.594 7820-7849/com.example.ayim.madoc W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-30 23:10:15.594 7820-7849/com.example.ayim.madoc W/System.err﹕ at java.lang.Thread.run(Thread.java:838)
这是我的堆栈显示的内容。来自数据库的值已获取,但未显示在列表视图中。我不知道该如何解决。谁能帮我?
我在这个Android Studio中仍然是新手。谢谢你。
这是我的Java代码。
public class ListPatient extends ListActivity {
//String intentID = getIntent().getExtras().getString("id");
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> patientList;
JSONArray patient = null;
TextView idShow;
private static final String list_url = "http://104.223.3.210/madoc/all_patient.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_patient);
idShow = (TextView) findViewById(R.id.id);
idShow.setText(getIntent().getExtras().getString("id"));
// Hashmap for ListView
patientList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllPatient().execute();
// Get listview
ListView lv = getListView();
}
class LoadAllPatient extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListPatient.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
List<NameValuePair> paras = new ArrayList<NameValuePair>();
// getting JSON string from URL
paras.add(new BasicNameValuePair("id", getIntent().getExtras().getString("id")));
JSONObject json1 = jParser.makeHttpRequest(list_url, "POST", paras);
Log.d("All Products: ", json1.toString());
// Checking for SUCCESS TAG
int success = json1.getInt("success");
if (success == 1) {
patient = json1.getJSONArray("patient");
// looping through All Products
for (int i = 0; i < patient.length(); i++) {
JSONObject c = patient.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("idPatient");
String name = c.getString("name");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("id", id);
map.put("name", name);
// adding HashList to ArrayList
patientList.add(map);
}
} else {
idShow.setText("No Patient");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ListPatient.this, patientList,
R.layout.list_patient, new String[] { "id",
"name"},
new int[] { R.id.id, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
这里抛出异常:
String id = c.getString("idPatient");
"idPatient"
JSON中没有密钥。尝试将其替换为"id"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句