【发布时间】:2018-12-09 05:06:47
【问题描述】:
我尝试从我的数据库中解析数据并将其显示在列表视图中。但是,由于在 logcat 中没有显示错误 volley 的作用,我不知道如何解决它。这是我的代码:
JsonResult
{"result":[{"namaBarang":"kabel","jumlahBarang":"5","tglKel":"2018-06-06"},{"namaBarang":"optical power meter","jumlahBarang":"5","tglKel":"0000-00-00"}]}
根据那个 json 结果,我尝试用 JsonObject 解析它,这就是我的 JsonObject 的样子。
活动
JsonObjectRequest bkRequest=new JsonObjectRequest(Request.Method.GET, >url, null ,new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONObject obj = response.getJSONObject("result"); BarangKeluar bk = new BarangKeluar(); bk.setNamaBarang(obj.getString("namaBarang")); bk.setJumlahBarang(obj.getString("jumlahBarang")); bk.setTglBarang(obj.getString("tglBarang")); bkList.add(bk) ; } catch (JSONException e) { e.printStackTrace(); } // notifying list adapter about data changes // so that it renders the list view with updated data adapter.notifyDataSetChanged(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); } }); // Adding request to request queue AppController.getInstance().addToRequestQueue(bkRequest);
但是在我将它推送到我的手机后,列表视图没有填充我的 json。
更新1 已经尝试尝试 pistolcaffe 和 IntelliJ Amiya 代码,但仍然无法正常工作。
Logcat
D/TextView: setTypeface with style : 0 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false I/System.out: (HTTPLog)-静态: isSBSettingEnabled false D/AbsListView: onsize 改变 D/Volley: [1] 2.onErrorResponse: AppController
但是我的 AppController 可以在 This 这个教程中正常工作。
标记的线程在Another 线程中重复。我对此进行了查找,不同之处在于大多数答案使用 HttpConnection。
已解决
最后,我解决了这个问题。
这是修复后的代码。
活动
//Create JsonObjectRequest JsonObjectRequest bkRequest = new JsonObjectRequest(Request.Method.GET, url, null,new Response.Listener<JSONObject>(){ @Override public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); try { JSONArray obj = response.getJSONArray("result"); for(int i=0;i< obj.length();i++) { JSONObject json = obj.getJSONObject(i); BarangKeluar bk = new BarangKeluar(); bk.setNamaBarang(json.getString("namaBarang")); bk.setJumlahBarang(json.getString("jumlahBarang")); bk.setTglBarang(json.getString("tglKel")); //Adding data into array bkList.add(bk); } } catch (JSONException e) { e.printStackTrace(); e.getMessage(); } // notifying list adapter about data changes // so that it renders the list view with updated data adapter.notifyDataSetChanged(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); } }); // Adding request to request queue AppController.getInstance().addToRequestQueue(bkRequest);
【问题讨论】:
-
嘿@Nilesh Rathod,我在类似的线程上查找,给出的大部分答案都是httpcon而不是凌空
标签: json android-volley