volley封装的方法如下:
public void RequestPost(Context context, String url,String uri, String tag, final Map<String, String> params, VolleyListenerInterface volleyListenerInterface) {
// 清除请求队列中的tag标记请求
SZSIApp.getRequestQueue().cancelAll(tag);
//允许安全证书运行
HTTPSTrustManager.allowAllSSL();
// 创建当前的POST请求,并将请求内容写入Map中
stringRequest = new Utf8StringRequest(Request.Method.POST, url+uri, volleyListenerInterface.responseListener(), volleyListenerInterface.errorListener()){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
};
// 为当前请求添加标记
stringRequest.setTag(tag);
// 将当前请求添加到请求队列中
SZSIApp.getRequestQueue().add(stringRequest);
// 重启当前请求队列
SZSIApp.getRequestQueue().start();
}
源码分析为什么调用了此方法之后会经常出现InterruptedIOException
实际上volley在初始化RequestQueue时,已经调用了start方法,代码如下:
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
Network network = new BasicNetwork(stack);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}
而调用start方法时, volley会调用stop方法,确保当前正在运行的所有dispatcher(分发)都已经停止了
public void stop() {
if (mCacheDispatcher != null) {
mCacheDispatcher.quit();
}
for (int i = 0; i < mDispatchers.length; i++) {
if (mDispatchers[i] != null) {
mDispatchers[i].quit();
}
}
}
停止dispatcher会调用quit方法
public void quit() {
mQuit = true;
interrupt();
}
interrupt方法如下,里面包含一个同步:
public void interrupt() {
// Interrupt this thread before running actions so that other
// threads that observe the interrupt as a result of an action
// will see that this thread is in the interrupted state.
nativeInterrupt();
synchronized (interruptActions) {
for (int i = interruptActions.size() - 1; i >= 0; i--) {
interruptActions.get(i).run();
}
}
}
所以重复调用start方法会造成网络请求失败的原因是:
线程在启动前被中断了,其他的线程会把它当做是因为操作而中断。