获取海量数据时Volley ProgressDialog stuck/freeze

Volley ProgressDialog stuck/freeze at the time of get huge amount of data

这是我的代码:

private void downloadSupplyTownData(final int townId2) {

    /*******************
     * Using Volley
     *******************/
    // Post params to be sent to the server
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("ID",townId2);
   CustomDialogClass.showProgressDialog(context,true); 


    JsonObjectRequest req = new JsonObjectRequest(Consts.baseUrl+Consts.townSupplyUrl, new JSONObject(params),
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                   try {
                       totalConsumerRecords =   Integer.parseInt(response.getString("TotalConsumerRecords").trim());
                       if(totalConsumerRecords>0)
                       {                               
                           /**For -----**/
                           JSONArray dtrArray   =   response.getJSONArray("xxx");
                           for(int i=0;i<dtrArray.length();i++)
                           {
                               JSONObject   dtrObj  =   dtrArray.getJSONObject(i);
                               supplyId1        =   Integer.parseInt(dtrObj.getString("SI"));
                               dtrId            =   Integer.parseInt(dtrObj.getString("DI"));
                               dtrImgUrl        =   dtrObj.getString("DIMG");
                               dtrCode          =   dtrObj.getString("DC");
                               assetPCode       =   dtrObj.getString("APC");
                               meterSN          =   dtrObj.getString("MSN");
                               System.out.println(dtrId);
                               db.addDtrInfo(new DTRBeanClass(dtrId,supplyId1,dtrCode,dtrImgUrl,assetPCode,meterSN));
                           }

                           /**For ----**/
                           JSONArray poleArray  =   response.getJSONArray("Pole");
                           for(int i=0;i<poleArray.length();i++)
                           {
                               JSONObject   poleObj =   poleArray.getJSONObject(i);
                               poleId           =   Integer.parseInt(poleObj.getString("PI"));
                               dtrId1           =   Integer.parseInt(poleObj.getString("DI"));
                               consumerCount    =   Integer.parseInt(poleObj.getString("ACA"));
                               poleImgUrl       =   poleObj.getString("PIMG");
                               poleCode         =   poleObj.getString("PC");
                               surveyerRemarks  =   poleObj.getString("RMS");
                               System.out.println(poleId);
                               db.addPoleInfo(new PoleBeanClass(poleId,dtrId1,poleCode,poleImgUrl,consumerCount,surveyerRemarks));
                           }

                           /**For ----**/
                           JSONArray consumerArray  =   response.getJSONArray("Supply");
                           for(int i=0;i<consumerArray.length();i++)
                           {
                               JSONObject   supplyObj   =   consumerArray.getJSONObject(i);
                               supplyId     =   Integer.parseInt(supplyObj.getString("SI"));
                               supplyTownId =   Integer.parseInt(supplyObj.getString("TI"));
                               supplyName   =   supplyObj.getString("SN");
                               System.out.println(supplyId);
                               db.addSupplierInfo(new SupplierChainBeanClass(supplyId,supplyTownId,supplyName));
                           }

                           CustomDialogClass.showProgressDialog(context,false);
                       }
                       else
                       {
                           CustomDialogClass.showProgressDialog(context,false);
                       }

                    } catch (JSONException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }                                                          
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   NetworkResponse networkResponse = error.networkResponse;
                   VolleyLog.e("Error: ", error.getMessage());
                   Log.d("Eroor", ""+networkResponse);
                   CustomDialogClass.showProgressDialog(context,false);
               }
           });

    // add the request object to the queue to be executed
    NameApplication.getInstance().addToRequestQueue(req);
}

这里通过CustomDialogClass.showProgressDialog(context,true);

显示和隐藏ProgressDialog

进度对话框先旋转 2-3 秒然后卡住。请帮我处理一下。

编辑

/** * 显示对话框 * */ @覆盖 受保护的对话框 onCreateDialog(int id) { 切换(id){ case progress_bar_type: // 我们将其设置为 0 pDialog = new ProgressDialog(这个); pDialog.setMessage("Downloading file. Please wait..."); pDialog.setIndeterminate(假); pDialog.setMax(100); pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pDialog.setCancelable(真); pDialog.show(); return pDialog; 默认: return空; } }

/**
 * Background Async Task to download file
 * */
class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread
     * Show Progress Bar Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(progress_bar_type);
    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        downloadSupplyTownData(townId);

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        pDialog.setProgress(Integer.parseInt(progress[0]));
   }

    /**
     * After completing background task
     * Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {            
        dismissDialog(progress_bar_type);

    }

}

并从视图中调用 AsynchTask new DownloadFileFromURL().execute();

Progress dialog spin first 2-3 seconds and then stuck.

因为解析 json 响应和 downloadConsumerData 方法从主 UI 线程发出 api 请求。

Volley onResponse 方法中作为回调方法在主 UI 线程中调用,当网络请求完成时。

要在 onResponse 方法中解析请求响应,请使用 AsyncTask class 并在 onPostExecute 方法中调用 ProgressDialog dismiss 方法,这将关闭 ProgressDialog 当 Volley 请求时,json 数据和 downloadConsumerData 方法作业的解析在后台 Thread

中完成

onResponse 方法启动 AsyncTask :

  @Override
      public void onResponse(JSONObject response) {
       new DownloadFileFromURL().execute(response);
  }

doInBackground过程中JSON数据:

  @Override
    protected String doInBackground(String... f_url) {
         totalConsumerRecords = Integer.parseInt(response.getString
                           ("TotalConsumerRecords").trim());
        // add all code here from onResponse
        downloadSupplyTownData(townId);

        return null;
    }