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() {  @Override  public void onResponse(String response) {  } }, new Response.ErrorListener() {  @Override  public void onErrorResponse(VolleyError error) {  } }) {  @Override  protected Map getParams()  throws AuthFailureError {   // 发送请求用到的一些参数   Map params = new HashMap();   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();  } }}

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多相关文章

  1. webkit中的javascript和android交互
  2. Android获取Java类名/文件名/方法名/行号
  3. android studio使用volley发送post和get请求
  4. Android获取联系人头像的方法
  5. android 异步加载图片缩略图
  6. Android(安卓)SQLiteDatabase中query、insert、update、delete方
  7. android关于手机和3.0版本以上平板去标题问题
  8. Android应用程序组件Content Provider的启动过程源代码分析(7)
  9. Android之——通用安装apk方法

随机推荐

  1. 为Android加入busybox工具
  2. [置顶] 我的Android进阶之旅------>Ubunt
  3. android单元测试
  4. android 电池(一):锂电池基本原理篇
  5. Android(安卓)SQLite数据库升级的问题
  6. 在Eclipse添加Android兼容包( v4、v7 app
  7. 【转】android开源项目---项目篇
  8. android SD卡检测和热插拔功能
  9. Android(安卓)O 下拉框增加自动亮度开关
  10. android的MediaPlayer的简介