使用Gson + Volley解析JSONArray没有得到响应

时间:2016-04-14 13:26:55

标签: java android json gson android-volley

所以我试图使用Google的Gson库和Volley for HTTP请求解析json中的一组对象。我的问题是,好像代码没有“点击”OnResponse来电。我已经尝试在函数中添加一个简单的Log打印输出,看它是否有效。

我的GsonRequest课程直接来自Google's Training Docs。我根据this question的答案构建了这些方法。

这是我的代码:

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

绝对没有错误。它正在授权请求,但OnResponse没有任何反应,它似乎忽略了该功能。

现在我尝试使用标准StringRequest进行排球,并且它可以完美运行,如下所示:

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

2 个答案:

答案 0 :(得分:0)

尝试在开头添加此行

RequestQueue helper = Volley.newRequestQueue(mContext);

答案 1 :(得分:0)

添加这些

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

如果您不想在缓存中保存响应,请添加此

requestQueue.add(stringRequest);

根据我个人的意见,如果你通过申请上下文,它会更好。