[英]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. 因此,我尝试使用Google的Gson库和Volley解析HTTP请求中的json对象数组。 My issue is it's as if the code isn't 'hitting' the
OnResponse
call . 我的问题是,好像代码没有“击中”
OnResponse
调用 。 I've tried adding a simple Log printout within the function just to see if it does anything. 我试图在函数中添加一个简单的Log打印输出,只是看它是否有作用。
My GsonRequest
class comes straight from Google's Training Docs . 我的
GsonRequest
类直接来自Google的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. 它正在授权请求,但是
OnResponse
什么也没有发生,它似乎忽略了该功能。
Now I've tried using a standard StringRequest
with volley and it works flawlessly, like this: 现在,我尝试将标准
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);
}
尝试在开始时添加此行
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. 根据我个人的看法,如果您通过应用程序上下文,则更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.