简体   繁体   中英

Parsing JSONArray using Gson + Volley not getting response

So I am trying to parse an array of objects from json using Google's Gson library and Volley for HTTP requests. My issue is it's as if the code isn't 'hitting' the OnResponse call . I've tried adding a simple Log printout within the function just to see if it does anything.

My GsonRequest class comes straight from Google's Training Docs . I constructed these methods based on an answer to this question .

This is my code:

private void runVolleyJson() throws AuthFailureError {
    GsonRequest<Meetings> getMeetings = new GsonRequest<Meetings>(AUTH_URL, Meetings.class, getHeaders(),
            createMyReqSuccessListener(),
            createMyReqErrorListener());
    helper.add(getMeetings);
}

private Response.Listener<Meetings> createMyReqSuccessListener() {
    return new Response.Listener<Meetings>() {
        @Override
        public void onResponse(Meetings response) {
            // NOTHING HAPPENS FROM HERE!
            try {
              Log.d("response", response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            // Do whatever you want to do with response;
            // Like response.tags.getListing_count(); etc. etc.
        }
    };
}

private Response.ErrorListener createMyReqErrorListener() {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // Do whatever you want to do with error.getMessage();
        }
    };
}

public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> map = new HashMap<>();
    map.put("Content-Type", "application/json;");
    map.put("Authorization", "Bearer <sometoken>");
    return map;
}

There is absolutely no error. It is authorizing the request, but nothing happens in OnResponse , it just seems to ignore that function.

Now I've tried using a standard StringRequest with volley and it works flawlessly, like this:

    private void runVolleyTest() {

    StringRequest request = new StringRequest(Request.Method.GET, AUTH_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jsonarray = new JSONArray(response);
                        for(int i = 0; i < jsonarray.length(); i++) {
                                                            Gson gson = new Gson();
                            Meeting m = gson.fromJson(jsonarray.get(i).toString(), Meeting.class);
                            Log.e("Meeting", m.getMeetingId() + " " + m.getStatus());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    ;
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    txtError(error);
                }
            }) {
        @Override
        public Map<String, String> getHeaders() {
            HashMap<String, String> map = new HashMap<>();
            map.put("Content-Type", "application/json;");
            map.put("Authorization", "Bearer <sometoken>");
            return map;
        }
    };

    //request.setPriority(Request.Priority.HIGH);
    helper.add(request);
}

尝试在开始时添加此行

RequestQueue helper = Volley.newRequestQueue(mContext);

Add these line

 RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);

if you don't want to the save response in cache memory then add this

requestQueue.add(stringRequest);

According to my personal opinion its better if you pass the application context.

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