ade*_*ine 15 android robolectric android-volley
我想让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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
Tho*_*man 19
我通过将RequestQueue的ResponseDelivery替换为不使用Looper.getMainLooper()但使用新Executor的ResponseDelivery来解决同样的问题.示例代码:
public static RequestQueue newRequestQueueForTest(final Context context, final OkHttpClient okHttpClient) {
final File cacheDir = new File(context.getCacheDir(), "volley");
final Network network = new BasicNetwork(new OkHttpStack(okHttpClient));
final ResponseDelivery responseDelivery = new ExecutorDelivery(Executors.newSingleThreadExecutor());
final RequestQueue queue =
new RequestQueue(
new DiskBasedCache(cacheDir),
network,
4,
responseDelivery);
queue.start();
return queue;
}
Run Code Online (Sandbox Code Playgroud)
注意:使用Robolectric-2.2-SNAPSHOT,以前的版本与Volley不搭配.
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
3256 次 |
| 最近记录: |