使用 Volley 过程中遇到的问题

本文主要介绍使用 Volley 过程中遇到的问题,错误提示:com.android.volley.NoConnectionError: java.io.InterruptedIOException,内容加载失败,问题出在重复调用queue.start()方法。

错误提示:com.android.volley.NoConnectionError: java.io.InterruptedIOException,然后就内容加载失败…

代码如下:

  1. private void getWxpayOrderInfo() {
  2. StringRequest stringRequest = new StringRequest(Request.Method.POST,
  3. Url, new Response.Listener<String>() {
  4. @Override
  5. public void onResponse(String response) {
  6. }
  7. }, new Response.ErrorListener() {
  8. @Override
  9. public void onErrorResponse(VolleyError error) {
  10. }
  11. }) {
  12. @Override
  13. protected Map<String, String> getParams()
  14. throws AuthFailureError {
  15. // 发送请求用到的一些参数
  16. Map<String, String> params = new HashMap<String, String>();
  17. params.put("id", "nameid");
  18. return params;
  19. }
  20. };
  21. stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000,
  22. DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  23. DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  24. queue.add(stringRequest);
  25. //queue.start(); //经过反复调试错误就出在这里,注释掉这里就可以了
  26. }

问题出在调用queue.start()方法之后,错误原因可以通过 Volley 源文件看到,以下是 Volley 官方文档中初始化RequestQueue的一段代码。

  1. /**
  2. * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
  3. *
  4. * @param context A {@link Context} to use for creating the cache dir.
  5. * @param stack An {@link HttpStack} to use for the network, or null for default.
  6. * @return A started {@link RequestQueue} instance.
  7. */
  8. public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
  9. File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
  10. String userAgent = "volley/0";
  11. try {
  12. String packageName = context.getPackageName();
  13. PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
  14. userAgent = packageName + "/" + info.versionCode;
  15. } catch (NameNotFoundException e) {
  16. }
  17. if (stack == null) {
  18. if (Build.VERSION.SDK_INT >= 9) {
  19. stack = new HurlStack();
  20. } else {
  21. // Prior to Gingerbread, HttpUrlConnection was unreliable.
  22. // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
  23. stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
  24. }
  25. }
  26. Network network = new BasicNetwork(stack);
  27. RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
  28. queue.start();//这里需要注意,原来在请求初始化的时候就已经调用了start方法
  29. return queue;
  30. }
  31. /**
  32. * Starts the dispatchers in this queue.
  33. */
  34. public void start() {
  35. stop(); // Make sure any currently running dispatchers are stopped.
  36. // Create the cache dispatcher and start it.
  37. mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
  38. mCacheDispatcher.start();
  39. // Create network dispatchers (and corresponding threads) up to the pool size.
  40. for (int i = 0; i < mDispatchers.length; i++) {
  41. NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
  42. mCache, mDelivery);
  43. mDispatchers[i] = networkDispatcher;
  44. networkDispatcher.start();
  45. }
  46. }
  47. /**
  48. * Stops the cache and network dispatchers.
  49. */
  50. public void stop() {
  51. if (mCacheDispatcher != null) {
  52. mCacheDispatcher.quit();
  53. }
  54. for (int i = 0; i < mDispatchers.length; i++) {
  55. if (mDispatchers[i] != null) {
  56. mDispatchers[i].quit();
  57. }
  58. }
  59. }
  60. /**
  61. * Forces this dispatcher to quit immediately. If any requests are still in
  62. * the queue, they are not guaranteed to be processed.
  63. */
  64. public void quit() {
  65. mQuit = true;
  66. interrupt();
  67. }
  68. public void interrupt() {
  69. // Interrupt this thread before running actions so that other
  70. // threads that observe the interrupt as a result of an action
  71. // will see that this thread is in the interrupted state.
  72. nativeInterrupt();
  73. synchronized (interruptActions) {
  74. for (int i = interruptActions.size() - 1; i >= 0; i--) {
  75. interruptActions.get(i).run();
  76. }
  77. }
  78. }

(完)