简体   繁体   中英

android parse jsonObject with volley

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?

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

使用Items而不是“”从响应JSONObject获取Items JSONArray

JSONArray mainjsonArray=response.getJSONArray("Items");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM