12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: com.android.volley.NoConnectionError: java.io.InterruptedIOException 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:125) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: Caused by: java.io.InterruptedIOException 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okio.Deadline.throwIfReached(Deadline.java:56) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okio.Okio$1.write(Okio.java:67) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okio.RealBufferedSink.flush(RealBufferedSink.java:154) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:130) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okhttp.internal.http.HttpTransport.flushRequest(HttpTransport.java:73) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:626) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:388) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:109) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93) 12-24 14:21:47.470 24220-24220/com.tongyan.tutelage W/System.err: ... 1 more
这个错误是你在不经意间导致的,或者是没有很好的去熟悉volley包的api导致的。
public void onRequest(String url){ if(isIntnetConn){ StringRequest request = new StringRequest(url, new Response.Listener<String>() { @Override public void onResponse(String s) { mListener.onSuccess(s); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { String errorMsg = ""; if(volleyError instanceof TimeoutError) { mListener.onError(); } else if(volleyError instanceof ServerError) { mListener.onError(); } else { mListener.onError(); errorMsg = mContext.getResources().getString(R.string.request_error); } } }); request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, 1.0f));//设置超时时间 mQueue.add(request); //mQueue.start(); }else{ Toast.makeText(mContext, "当前的网络连接不可用", Toast.LENGTH_SHORT).show(); mListener.onIntnetDisConn(); } }
只是因为加了下面这句,bug如期而至。
//mQueue.start();
其实,在RequestQueue实例化的时候,就已经有start了。
private RequestQueue mQueue; private Context mContext; public HttpServiceUtils(HttpResquestListener listener,Context context){ mListener = listener; mContext = context; mQueue = Volley.newRequestQueue(context); }
public class Volley { private static final String DEFAULT_CACHE_DIR = "volley"; public Volley() { } public static RequestQueue newRequestQueue(Context context, HttpStack stack) { File cacheDir = new File(context.getCacheDir(), "volley"); String userAgent = "volley/0"; try { String network = context.getPackageName(); PackageInfo queue = context.getPackageManager().getPackageInfo(network, 0); userAgent = network + "/" + queue.versionCode; } catch (NameNotFoundException var6) { ; } if(stack == null) { if(VERSION.SDK_INT >= 9) { stack = new HurlStack(); } else { stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); } } BasicNetwork network1 = new BasicNetwork((HttpStack)stack); RequestQueue queue1 = new RequestQueue(new DiskBasedCache(cacheDir), network1); queue1.start(); return queue1; } public static RequestQueue newRequestQueue(Context context) { return newRequestQueue(context, (HttpStack)null); } }
queue1.start(); 已经在实例化RequestQueue的时候,就被调用了。
https://stackoverflow.com/questions/31158858/random-com-android-volley-noconnection-error-java-io-interruptedioexception-st/31181611#31181611