我试图使用json从数据库中检索数据。我成功获取数据但它没有显示在我的列表视图中。
这是logcat:
04-13 20:01:42.205 5230-5252/? I/Sucess: {"keranjang":[{"id_keranjang":"1","kd_inventaris":"AIKC100-2","judul_buku":"judul_buku"}],"success":1,"message":"Ambil data di tabel keranjang sukses"}
04-13 20:01:42.205 5230-5252/? W/System.err: org.json.JSONException: No value for id_keranjang
04-13 20:01:42.205 5230-5252/? W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
04-13 20:01:42.205 5230-5252/? W/System.err: at org.json.JSONObject.getInt(JSONObject.java:478)
04-13 20:01:42.205 5230-5252/? W/System.err: at com.example.yong.p_akakom.PageFragment2$Ambil_Buku_Keranjang.doInBackground(PageFragment2.java:86)
04-13 20:01:42.205 5230-5252/? W/System.err: at com.example.yong.p_akakom.PageFragment2$Ambil_Buku_Keranjang.doInBackground(PageFragment2.java:69)
04-13 20:01:42.206 5230-5252/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-13 20:01:42.206 5230-5252/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-13 20:01:42.206 5230-5252/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-13 20:01:42.206 5230-5252/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-13 20:01:42.206 5230-5252/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-13 20:01:42.206 5230-5252/? W/System.err: at java.lang.Thread.run(Thread.java:818)
AsyncTask:
class Ambil_Buku_Keranjang extends AsyncTask<String,String,String> {
ArrayList<HashMap<String, String>> daftarBukuKeranjang = new ArrayList<HashMap<String, String>>();
@Override
protected String doInBackground(String... params) {
String id = params[0];
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair(TAG_ANGGOTA,id));
json = jsonParser.makeHttpRequest(URL,"POST",pairs);
try{
buku_keranjang = json.getJSONArray("keranjang");
success = json.getInt(TAG_SUCCESS);
if(success == 1){
Log.d("Success","Sukess tampilkan data buku keranjang");
Log.i("Sucess",json.toString());
for(int i=0;i<buku_keranjang.length();i++){
JSONObject c = new JSONObject();
HashMap<String,String> map = new HashMap<String,String>();
int id_keranjang = c.getInt(TAG_KERANJANG);
String kd_inventaris = c.getString(TAG_INVENTARIS);
String judul_buku = c.getString(TAG_JUDUL);
map.put(TAG_KERANJANG,String.valueOf(id_keranjang));
map.put(TAG_INVENTARIS,kd_inventaris);
map.put(TAG_JUDUL,judul_buku);
daftarBukuKeranjang.add(map);
}
}
}catch (JSONException e){
e.printStackTrace();
}
return TAG_MESSAGE;
}
@Override
protected void onPostExecute(String s) {
if(s != null && success==1){
adapter = new SimpleAdapter(getActivity().getApplicationContext(),daftarBukuKeranjang,
R.layout.list_keranjang,new String[]{TAG_KERANJANG,TAG_INVENTARIS,TAG_JUDUL},
new int[]{R.id.id_keranjang,R.id.kd_inventaris_keranjang,R.id.judul_buku_keranjang});
listPesan.setAdapter(adapter);
}
}
}
我的列表布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/id_keranjang"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/kd_inventaris_keranjang"
android:layout_below="@+id/id_keranjang"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/judul_buku_keranjang"
android:layout_below="@+id/kd_inventaris_keranjang"
android:layout_alignParentStart="true" />
</RelativeLayout>
请帮助......谢谢..
答案 0 :(得分:2)
JSONObject c = new JSONObject();
for循环中的这一行是错误的。从阵列获取对象时,您不会创建新对象。相反,它会像这样
JSONObject c = buku_keranjang.getJSONObject(i);
答案 1 :(得分:1)
由于json响应中不存在您的错误日志密钥。为避免此类型错误,您可以使用可选字符串。如下面=&gt;
for(int i=0;i<buku_keranjang.length();i++){
JSONObject c = buku_keranjang.getJSONObject(i);
HashMap<String,String> map = new HashMap<String,String>();
// see here
int id_keranjang = c.optInt(TAG_KERANJANG);
String kd_inventaris = c.optString(TAG_INVENTARIS);
String judul_buku = c.optString(TAG_JUDUL);
map.put(TAG_KERANJANG,String.valueOf(id_keranjang));
map.put(TAG_INVENTARIS,kd_inventaris);
map.put(TAG_JUDUL,judul_buku);
daftarBukuKeranjang.add(map);
}
答案 2 :(得分:0)
更改for循环中的代码
for(int i=0;i<buku_keranjang.length();i++){
JSONObject c =buku_keranjang.getJSONObject(i);
HashMap<String,String> map = new HashMap<String,String>();
int id_keranjang = c.getInt(TAG_KERANJANG);
String kd_inventaris = c.getString(TAG_INVENTARIS);
String judul_buku = c.getString(TAG_JUDUL);
map.put(TAG_KERANJANG,String.valueOf(id_keranjang));
map.put(TAG_INVENTARIS,kd_inventaris);
map.put(TAG_JUDUL,judul_buku);
daftarBukuKeranjang.add(map);
}