Android 使用volley過(guò)程中遇到的問(wèn)題解決辦法
Android 使用volley過(guò)程中遇到的問(wèn)題解決辦法
本文主要介紹使用 volley 過(guò)程中遇到的問(wèn)題,錯(cuò)誤提示:
com.android.volley.NoConnectionError: java.io.InterruptedIOException”,內(nèi)容加載失敗,問(wèn)題出在重復(fù)調(diào)用 queue.start() 方法。
錯(cuò)誤提示:com.android.volley.NoConnectionError: java.io.InterruptedIOException”,然后就內(nèi)容加載失敗。。。、
代碼如下:
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 { // 發(fā)送請(qǐng)求用到的一些參數(shù) 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(); //經(jīng)過(guò)反復(fù)調(diào)試錯(cuò)誤就出在這里,注釋掉這里就可以了 }
問(wèn)題出在調(diào)用 queue.start() 方法之后,錯(cuò)誤原因可以通過(guò) 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();//這里需要注意,原來(lái)在請(qǐng)求初始化的時(shí)候就已經(jīng)調(diào)用了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(); } } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android 網(wǎng)絡(luò)請(qǐng)求框架Volley實(shí)例詳解
- Android 開(kāi)發(fā)中Volley詳解及實(shí)例
- Android Volley框架使用方法詳解
- Android中Volley框架進(jìn)行請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù)的使用
- Android Volley框架全面解析
- 深入解讀Android的Volley庫(kù)的功能結(jié)構(gòu)
- Android的HTTP類(lèi)庫(kù)Volley入門(mén)學(xué)習(xí)教程
- Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存
相關(guān)文章
Native.js獲取監(jiān)聽(tīng)開(kāi)關(guān)等操作Android藍(lán)牙設(shè)備實(shí)例代碼
本文為大家分享了Native.js對(duì)Android藍(lán)牙設(shè)備的操作實(shí)例代碼包括:監(jiān)聽(tīng)藍(lán)牙開(kāi)關(guān)狀態(tài),開(kāi)啟關(guān)閉藍(lán)牙,獲取藍(lán)牙設(shè)備列表,藍(lán)牙連接票據(jù)打印機(jī)2018-09-09Android開(kāi)發(fā)實(shí)現(xiàn)自定義Toast、LayoutInflater使用其他布局示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)自定義Toast、LayoutInflater使用其他布局,涉及Android自定義Toast與界面布局相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android自定義View實(shí)現(xiàn)內(nèi)存清理加速球效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)內(nèi)存清理加速球的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android詳細(xì)講解谷歌推出的官方二維碼掃描庫(kù)
Google推出的官方二維碼掃描庫(kù)你知道嗎?還不知道就落伍咯!本篇文字帶你了解google二維碼掃描庫(kù)的詳細(xì)情況與使用,還不知道的朋友快來(lái)看看吧2022-03-03Android使用ViewStub實(shí)現(xiàn)布局優(yōu)化方法示例
這篇文章主要為大家介紹了Android使用ViewStub實(shí)現(xiàn)布局優(yōu)化方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android RxJava創(chuàng)建操作符Interval
這篇文章主要為大家詳細(xì)介紹了Android RxJava創(chuàng)建操作符Interval的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12android listview實(shí)現(xiàn)新聞列表展示效果
這篇文章主要為大家詳細(xì)介紹了android listview實(shí)現(xiàn)新聞列表展示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03android ListView的右邊滾動(dòng)滑塊啟用方法 分享
android ListView的右邊滾動(dòng)滑塊啟用方法 分享,需要的朋友可以參考一下2013-05-05Android視頻處理之動(dòng)態(tài)時(shí)間水印效果
這篇文章主要A為大家詳細(xì)介紹了Android視頻處理之動(dòng)態(tài)時(shí)間水印效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09