Android使用volley過程中遇到的問題解決辦法

Android 使用volley過程中遇到的問題解決辦法

我們提供的服務有:成都網(wǎng)站建設、網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、石阡ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的石阡網(wǎng)站制作公司

本文主要介紹使用 volley 過程中遇到的問題,錯誤提示:

com.android.volley.NoConnectionError: java.io.InterruptedIOException”,內(nèi)容加載失敗,問題出在重復調(diào)用 queue.start() 方法。

錯誤提示: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ā)送請求用到的一些參數(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)過反復調(diào)試錯誤就出在這里,注釋掉這里就可以了
}

問題出在調(diào)用 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();//這里需要注意,原來在請求初始化的時候就已經(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();
  }
 }
}

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

名稱欄目:Android使用volley過程中遇到的問題解決辦法
分享URL:http://muchs.cn/article10/pidcgo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、服務器托管、網(wǎng)站導航、企業(yè)網(wǎng)站制作網(wǎng)站設計公司、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設計公司