我有以下代码,使用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实现为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
如何克服?如果我收到此错误,可以重新申请吗?
答案 0 :(得分:2)
尝试使用以下代码:
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 。