Parsing JSON(from select database) using Volley [duplicate]


Parsing JSON(from select database) using Volley <Solved> [duplicate]



This question already has an answer here:



So i try to parsing data from my database and show it on listview. However since in logcat does't show what error volley does, i dont know how to solve it. Here's my code:



JsonResult


{"result":[{"namaBarang":"kabel","jumlahBarang":"5","tglKel":"2018-06-06"},{"namaBarang":"optical power meter","jumlahBarang":"5","tglKel":"0000-00-00"}]}



From that json result, i try to parse it with JsonObject, and here's what my JsonObject looks like.



Activity


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);



But after i push it on my phone, the listview not populate with my json.



UPDATE1
Already trying to try pistolcaffe and IntelliJ Amiya code but still not working.



Logcat



D/TextView: setTypeface with style : 0 I/System.out: (HTTPLog)-Static:
isSBSettingEnabled false I/System.out: (HTTPLog)-Static:
isSBSettingEnabled false D/AbsListView: onsize change D/Volley: [1]
2.onErrorResponse: AppController



But my AppController work fine with This this tutorial.



Marked threads duplicate in Another threads. I look up on this and the difference is most of answer use HttpConnection.



EDIT 2
Finally, i fixed this.



Here's the code after fix, hope it can help somebody else



Activity



//Create JsonObjectRequest
JsonObjectRequest bkRequest = new JsonObjectRequest(Request.Method.GET, url, null,new
Response.Listener(){
@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);



Thank you for helping guys



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Hey @Nilesh Rathod , i do lookup on similar thread, and most of the answer given were httpcon not a volley
– tupir
yesterday




2 Answers
2



Expected Error is



JSONArray cannot be converted to JSONObject


result


JSONArray


tglBarang



Don't


JSONObject obj = response.getJSONObject("result");



Do


JSONArray jsonArray = response.getJSONArray("result");



RECTIFIED CODE


JSONArray jsonArray = response.getJSONArray("result");
for(int i=0;i<jsonArray.length;i++)
{
JSONObject json = jsonArray.getJSONObject(i);
String namaBarang = json.getString("namaBarang");
String jumlahBarang = json.getString("jumlahBarang");
String tglKel = json.getString("tglKel");

BarangKeluar bk = new BarangKeluar();
bk.setNamaBarang(namaBarang);
bk.setJumlahBarang(jumlahBarang);
bk.setTglBarang(tglKel);
bkList.add(bk)
}





Clean-Rebuild and Run.
– IntelliJ Amiya
Jun 30 at 3:25


Clean-Rebuild and Run





do i need to change my JsonObjectRequest method into JsonArrayRequest? i do try this answer, and still not work somehow.
– tupir
Jun 30 at 11:05




you missied JSONArray and "tglBarang" -> "tglKel"


JSONArray obj = response.getJSONArray("result");
for (int i = 0; i < obj.length(); i++) {
String namaBarang = obj.getJSONObject(i).getString("namaBarang");
String jumlahBarang = obj.getJSONObject(i).getString("jumlahBarang");
String tglKel = obj.getJSONObject(i).getString("tglKel");
}





already tried ur code and @IntelliJ Amiya code, both still not working. Do i need to change my method instead into JsonArrayRequest ?
– tupir
Jun 30 at 11:14

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV