Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

json - JSONArray response with Volley for Android

I am posting some data into the database using Volley and I get the following jsonarray response.

[
  {
  "nickname":"panikos",
  "username":"panikos@gmail.com",
  "user_type":"LEADER",
  "latest_steps":"0"
  }
]

This is a sample of my code that unfortunately doesn't log out or debug the variable of "nickname" object:(.

final JsonArrayRequest jsonObjReq1 = new 
JsonArrayRequest(AppConfig.URL_GET_TEAM, jsonObject,
            new com.android.volley.Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    Log.d("TAG", response.toString());

                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        for(int i=0;i<jsonArray.length();i++){
                            JSONObject jresponse = 
                jsonArray.getJSONObject(i);
                String nickname =                                 
           jresponse.getString("nickname");
                            Log.d("nickname",nickname);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //pDialog.dismiss();

                }
            }, new com.android.volley.Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("TAG", "Error: " + error.getMessage());
            //pDialog.dismiss();

        }
    }) {

        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }


    };

Any ideas? Am I missing something?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I the problem might be - you are already getting response as a JSONArray.

So, you can Call

JSONObject jresponse = response.getJSONObject(0);

and if you have more than 1 object in response, then

for(int i = 0; i < response.length(); i++){
    JSONObject jresponse = response.getJSONObject(i);
    String nickname = jresponse.getString("nickname");
    Log.d("nickname", nickname);
}

Remove this :

               try {
                    JSONArray jsonArray = new JSONArray(response);

                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jresponse = 
            jsonArray.getJSONObject(i);
            String nickname =                                 
       jresponse.getString("nickname");
                        Log.d("nickname",nickname);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

and add :

try {
    JSONObject jresponse = response.getJSONObject(0);
    String nickname = jresponse.getString("nickname");
    Log.d("nickname",nickname);
}catch (JSONException e) {
    e.printStackTrace();
}

The Code looks good, however i think you might be missing a call to add jsonObjReq1 in the request queue. I would suggest to use Singleton Pattern.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...