我有以下代码使用齐射向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实现为like this:
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
如何克服?有什么办法可以重新请求我是否遇到此错误?
最佳答案
尝试按以下方式使用此代码:
import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
public class OkHttpStack extends HurlStack {
private final OkUrlFactory mFactory;
public OkHttpStack() {
this(new OkHttpClient());
}
public OkHttpStack(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
mFactory = new OkUrlFactory(client);
}
}
您可以引用我的code来实现 OkHttpStack
*更新:
也许您可以检查何时自定义requestQueue代码。您是否在代码中添加了 requestQueue.start()?如果您这样做了,它肯定会显示 interruptedioexception 。