Android 使用volley过程中遇到的问题解决办法
程序员文章站
2023-12-05 23:41:28
android 使用volley过程中遇到的问题解决办法
本文主要介绍使用 volley 过程中遇到的问题,错误提示:
com.android.volley.nocon...
android 使用volley过程中遇到的问题解决办法
本文主要介绍使用 volley 过程中遇到的问题,错误提示:
com.android.volley.noconnectionerror: java.io.interruptedioexception”,内容加载失败,问题出在重复调用 queue.start() 方法。
错误提示:com.android.volley.noconnectionerror: java.io.interruptedioexception”,然后就内容加载失败。。。、
代码如下:
private void getwxpayorderinfo() { stringrequest stringrequest = new stringrequest(request.method.post, url, new response.listener<string>() { @override public void onresponse(string response) { } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override protected map<string, string> getparams() throws authfailureerror { // 发送请求用到的一些参数 map<string, string> params = new hashmap<string, string>(); params.put("id", "nameid"); return params; } }; stringrequest.setretrypolicy(new defaultretrypolicy(10000, defaultretrypolicy.default_max_retries, defaultretrypolicy.default_backoff_mult)); queue.add(stringrequest); //queue.start(); //经过反复调试错误就出在这里,注释掉这里就可以了 }
问题出在调用 queue.start() 方法之后,错误原因可以通过 volley 源文件看到,以下是 volley 官方文档中初始化 requestqueue 的一段代码。
/** * creates a default instance of the worker pool and calls {@link requestqueue#start()} on it. * * @param context a {@link context} to use for creating the cache dir. * @param stack an {@link httpstack} to use for the network, or null for default. * @return a started {@link requestqueue} instance. */ 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();//这里需要注意,原来在请求初始化的时候就已经调用了start方法 return queue; } /** * starts the dispatchers in this queue. */ public void start() { stop(); // make sure any currently running dispatchers are stopped. // create the cache dispatcher and start it. mcachedispatcher = new cachedispatcher(mcachequeue, mnetworkqueue, mcache, mdelivery); mcachedispatcher.start(); // create network dispatchers (and corresponding threads) up to the pool size. for (int i = 0; i < mdispatchers.length; i++) { networkdispatcher networkdispatcher = new networkdispatcher(mnetworkqueue, mnetwork, mcache, mdelivery); mdispatchers[i] = networkdispatcher; networkdispatcher.start(); } } /** * stops the cache and network dispatchers. */ public void stop() { if (mcachedispatcher != null) { mcachedispatcher.quit(); } for (int i = 0; i < mdispatchers.length; i++) { if (mdispatchers[i] != null) { mdispatchers[i].quit(); } } } /** * forces this dispatcher to quit immediately. if any requests are still in * the queue, they are not guaranteed to be processed. */ public void quit() { mquit = true; 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(); } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!