Android Volley NoConnectionError: java.io.InterruptedIOException
Android Volley NoConnectionError: java.io.InterruptedIOException
我有以下代码使用 volley 向网络服务发出请求:
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.write(Okio.java:73)
00.483 com.ex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout.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.write(Okio.java:73)
00.589 com.ex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout.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
如何克服?如果我收到此错误,有什么方法可以重新请求?
在此之前我正在执行另一个请求,实际上它给出了错误。我也将上面的 requestQueue
代码实现到那个代码中,现在没有错误。上面的代码(我的)很酷。
尝试使用此代码如下:
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
*更新:
Maybe you can check when you customize the requestQueue code. Did you add requestQueue.start() in your code? If you did it,and then it will show the interruptedioexception certainly.
我有以下代码使用 volley 向网络服务发出请求:
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.write(Okio.java:73)
00.483 com.ex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout.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.write(Okio.java:73)
00.589 com.ex W/System.err﹕ at com.android.okhttp.okio.AsyncTimeout.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
如何克服?如果我收到此错误,有什么方法可以重新请求?
在此之前我正在执行另一个请求,实际上它给出了错误。我也将上面的 requestQueue
代码实现到那个代码中,现在没有错误。上面的代码(我的)很酷。
尝试使用此代码如下:
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
*更新:
Maybe you can check when you customize the requestQueue code. Did you add requestQueue.start() in your code? If you did it,and then it will show the interruptedioexception certainly.