我一直在使用Volley在我的应用上执行网络任务。这一次,它经常给我不一致的回应。有时错误响应,有时是正确的。这是我的代码:
public void requestGet(final Context context, final String url){
request = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
obj = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
error.printStackTrace();
}
});
request.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(request);
}
以下是有时发生错误的logcat:
32646-32646/com.example.apps W/System.err: com.android.volley.NoConnectionError: java.net.ProtocolException: Unexpected status line: 0
32646-32646/com.example.apps W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
32646-32646/com.example.apps W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
32646-32646/com.example.apps W/System.err: Caused by: java.net.ProtocolException: Unexpected status line: 0
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:782)
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
32646-32646/com.example.apps W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:521)
32646-32646/com.example.apps W/System.err: at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110)
32646-32646/com.example.apps W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
32646-32646/com.example.apps W/System.err: ... 1 more
一些信息:
有什么建议吗?谢谢。
答案 0 :(得分:0)
request.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
这里的第一个参数是超时,它应该大于0,例如10000(10秒)。
答案 1 :(得分:0)
尝试此重试政策: -
request.setRetryPolicy(new DefaultRetryPolicy(5*1000,-1,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));