Reputation: 295
I first time try to parse json with volley. i found some examples and i learned how to use it. but I have one problem.... i have like this json
{
"Items ":
[
{
"Branch" :"mybranch",
"City" : "London"
},
{
"Branch" :"mybranch1",
"City" : "Paris"
},
{
"Branch" :"mybranch2",
"City" : "NY"
}
]
}
and this is a my parser code
private void makeGetDictionaries13Request() {
showpDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
urlgetbranchlist, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("TAG", response.toString());
try {
JSONArray mainjsonArray=response.getJSONArray("");
for (int i = 0; i < mainjsonArray.length(); i++) {
JSONObject j_object=mainjsonArray.getJSONObject(i);
System.out.println();
Log.e("City", j_object.getString("City"));
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
15000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
I can show jsonobject result in log but i have volley error
no value for
for my option i have something wrong in parse son..what am i doing wrong?
Upvotes: 0
Views: 1331
Reputation: 132982
Use Items
instead of "" for getting Items JSONArray
from response JSONObject
:
JSONArray mainjsonArray=response.getJSONArray("Items");
Upvotes: 0
Reputation: 128428
There is a minor mistake in getting JSONArray from the JSONObject response. You actually forgot to mention the array name which is items
.
Mistake:
JSONArray mainjsonArray=response.getJSONArray("");
Correct:
JSONArray mainjsonArray=response.getJSONArray("Items");
Upvotes: 1