我有以下代码,使用Volley向Web服务发出请求:
有时我会遇到以下错误:
如何克服这个问题?如果我遇到此错误,有什么重新请求的方法吗?
RequestQueue requestQueue = Volley.newRequestQueue(getActivity(),new OkHttpStack());
GsonRequest<myData> request = new GsonRequest<myData>(Method.POST,Config.MYPOSTLINK, myData.class, getSuccessListener, getErrorListener);
request.setParams("Token", token);
requestQueue.add(request);
我已经按照这个样子实现了OkHttpStack.java:
public class OkHttpStack extends HurlStack {
private final OkHttpClient client;
public OkHttpStack() {
this(new OkHttpClient());
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
this.client = client;
}
@Override protected HttpURLConnection createConnection(URL url) throws IOException {
return new OkUrlFactory(client).open(url);
}
}
有时我会遇到以下错误:
00.482 com.ex W/System.err﹕ com.android.volley.NoConnectionError: java.io.InterruptedIOException: thread interrupted
00.483 com.ex W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
00.483 com.ex W/System.err﹕ at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
00.483 com.ex W/System.err﹕ Caused by: java.io.InterruptedIOException: thread interrupted
00.483 com.ex W/System.err﹕ at com.android.okhttp.okio.Timeout.throwIfReached(Timeout.java:145)
00.483 com.ex W/System.err﹕ at com.android.okhttp.okio.Okio$1.write(Okio.java:73)
00.483 com.ex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
00.483 com.ex W/System.err﹕ at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
00.483 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
00.483 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
00.483 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:902)
00.483 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
00.484 com.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
00.484 com.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
00.484 com.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
00.484 com.ex W/System.err﹕ at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:109)
00.484 com.ex W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
00.484 com.ex W/System.err﹕ ... 1 more
00.589 com.ex W/System.err﹕ com.android.volley.NoConnectionError: java.io.InterruptedIOException: thread interrupted
00.589 com.ex W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
00.589 com.ex W/System.err﹕ at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
00.589 com.ex W/System.err﹕ Caused by: java.io.InterruptedIOException: thread interrupted
00.589 com.ex W/System.err﹕ at com.android.okhttp.okio.Timeout.throwIfReached(Timeout.java:145)
00.589 com.ex W/System.err﹕ at com.android.okhttp.okio.Okio$1.write(Okio.java:73)
00.589 com.ex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
00.589 com.ex W/System.err﹕ at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
00.589 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
00.589 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
00.589 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:902)
00.589 com.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
00.590 com.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
00.590 com.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
00.590 com.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
00.590 com.ex W/System.err﹕ at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:109)
00.590 com.ex W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
00.590 com.ex W/System.err﹕ ... 1 more
如何克服这个问题?如果我遇到此错误,有什么重新请求的方法吗?