我正在尝试让 Volley 与 Robolectric 合作。我可以看到我的 HTTP 请求被调用,并且 parseNetworkResponse 被调用(我正在发送 JsonRequest 的自定义子类),但我的监听器没有被调用。有什么建议吗?这是一个代码示例:
@Test
public void testTypeAheadClient() throws Exception {
Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
//mRemoteRequestQueue and mCustomRequest are set up previously
mRemoteRequestQueue.add(mCustomRequest);
}
private static class CustomRequest extends JsonRequest<MyObject> {
public CustomRequest(String url,
Response.Listener<MyObject> listener,
Response.ErrorListener errorListener) {
super(Request.Method.GET, url, null, listener, errorListener);
}
@Override
protected Response<MyObject> parseNetworkResponse(NetworkResponse response) {
System.out.println("in parseNetworkResponse");
try {
MyObject myObject = new MyObject(new JSONArray(new String(response.data, "UTF-8")));
return Response.success(myObject, HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
}