凌空ProgressDialog卡/冻结在时间获取大量的数据 [英] Volley ProgressDialog stuck/freeze at the time of get huge amount of data

查看:264
本文介绍了凌空ProgressDialog卡/冻结在时间获取大量的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

 私人无效downloadSupplyTownData(最终诠释townId2){    / *******************
     *使用排球
     ******************* /
    //发布PARAMS被发送到服务器
    HashMap的<弦乐,对象> PARAMS =新的HashMap<弦乐,对象>();
    params.put(ID,townId2);
   CustomDialogClass.showProgressDialog(上下文,真);
    JsonObjectRequest REQ =新JsonObjectRequest(Consts.baseUrl + Consts.townSupplyUrl,新的JSONObject(PARAMS)
           新Response.Listener<&JSONObject的GT;(){
               @覆盖
               公共无效onResponse(JSONObject的响应){
                   尝试{
                       totalConsumerRecords =的Integer.parseInt(response.getString(TotalConsumerRecords)修剪());
                       如果(totalConsumerRecords大于0)
                       {
                           / **对于----- ** /
                           JSONArray dtrArray = response.getJSONArray(XXX);
                           的for(int i = 0; I< dtrArray.length();我++)
                           {
                               JSONObject的dtrObj = dtrArray.getJSONObject(I)
                               supplyId1 =的Integer.parseInt(dtrObj.getString(SI));
                               dtrId =的Integer.parseInt(dtrObj.getString(DI));
                               dtrImgUrl = dtrObj.getString(DIMG);
                               DTR code = dtrObj.getString(DC);
                               assetP code = dtrObj.getString(APC);
                               meterSN = dtrObj.getString(MSN);
                               的System.out.println(dtrId);
                               db.addDtrInfo(新DTRBeanClass(dtrId,supplyId1,DTR code,dtrImgUrl,assetP code,meterSN));
                           }                           / **对于---- ** /
                           JSONArray poleArray = response.getJSONArray(极点);
                           的for(int i = 0; I< poleArray.length();我++)
                           {
                               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);
                               极code = poleObj.getString(PC);
                               surveyerRemarks = poleObj.getString(RMS);
                               的System.out.println(poleId);
                               db.addPoleInfo(新PoleBeanClass(poleId,dtrId1,极code,poleImgUrl,consumerCount,surveyerRemarks));
                           }                           / **对于---- ** /
                           JSONArray consumerArray = response.getJSONArray(供应);
                           的for(int i = 0; I< consumerArray.length();我++)
                           {
                               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(新SupplierChainBeanClass(s​​upplyId,supplyTownId,supplyName));
                           }                           CustomDialogClass.showProgressDialog(背景下,FALSE);
                       }
                       其他
                       {
                           CustomDialogClass.showProgressDialog(背景下,FALSE);
                       }                    }赶上(JSONException E1){
                        // TODO自动生成catch块
                        e1.printStackTrace();
                    }
               }
           },新Response.ErrorListener(){
               @覆盖
               公共无效onErrorResponse(VolleyError错误){
                   NetworkResponse networkResponse = error.networkResponse;
                   VolleyLog.e(错误,error.getMessage());
                   Log.d(Eroor,+ networkResponse);
                   CustomDialogClass.showProgressDialog(背景下,FALSE);
               }
           });    //添加请求对象到队列将要执行
    。NameApplication.getInstance()addToRequestQueue(REQ);
}

在这里展示和 CustomDialogClass.showProgressDialog隐藏ProgressDialog(背景下,真正的);

进度对话框旋前2-3秒,然后卡住。请帮我处理这个问题。

修改

/ **
     *显示对话框
     * * /
    @覆盖
    保护对话框onCreateDialog(INT ID){
        开关(ID){
        案例progress_bar_type://我们设置它为0
            pDialog =新ProgressDialog(本);
            pDialog.setMessage(下载文件,请稍候...);
            pDialog.setIndeterminate(假);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(真);
            pDialog.show();
            返回pDialog;
        默认:
            返回null;
        }
    }

  / **
 *背景异步任务来下载文件
 * * /
类DownloadFileFromURL扩展的AsyncTask<字符串,字符串,字符串> {    / **
     *启动后台线程之前
     *显示进度条对话框
     * * /
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        的ShowDialog(progress_bar_type);
    }    / **
     在后台线程下载*文件
     * * /
    @覆盖
    保护字符串doInBackground(字符串... f_url){
        downloadSupplyTownData(townId);        返回null;
    }    / **
     *更新进度条
     * * /
    保护无效onProgressUpdate(字符串...进度){
        //设置进度百分比
        pDialog.setProgress(的Integer.parseInt(进展[0]));
   }    / **
     *在完成后台任务之后
     *辞退进度对话框
     * ** /
    @覆盖
    保护无效onPostExecute(字符串FILE_URL){
        dismissDialog(progress_bar_type);    }}

和从视图中调用AsynchTask 新DownloadFileFromURL()执行();


解决方案

  

进度对话框旋前2-3秒,然后卡住了。


由于JSON响应的解析和 downloadConsumerData 方法实现从主UI线程API请求。

排球 onResponse 方法调用作为在网络请求完成主UI线程回调方法。

有关解析在 onResponse 方法使用的AsyncTask 类请求响应并调用 ProgressDialog 驳回 onPostExecute 方法方法,它会关闭 ProgressDialog 时凌空请求,JSON数据和分析 downloadConsumerData 方法工作在后台线程中完成的。

开始的AsyncTask从 onResponse 方法:

  @覆盖
      公共无效onResponse(JSONObject的响应){
       新DownloadFileFromURL()执行(响应);
  }

doInBackground 处理JSON数据:

  @覆盖
    保护字符串doInBackground(字符串... f_url){
         totalConsumerRecords =的Integer.parseInt(response.getString
                           (TotalConsumerRecords)修剪());
        //此处添加所有code从onResponse
        downloadSupplyTownData(townId);        返回null;
    }

Here is my code:

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);
}

Here show and hide ProgressDialog by CustomDialogClass.showProgressDialog(context,true);

Progress dialog spin first 2-3 seconds and then stuck. Please help me out to handle this.

EDIT

/** * Showing Dialog * */ @Override protected Dialog onCreateDialog(int id) { switch (id) { case progress_bar_type: // we set this to 0 pDialog = new ProgressDialog(this); pDialog.setMessage("Downloading file. Please wait..."); pDialog.setIndeterminate(false); pDialog.setMax(100); pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pDialog.setCancelable(true); pDialog.show(); return pDialog; default: return null; } }

/**
 * 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);

    }

}

And from view call the AsynchTask new DownloadFileFromURL().execute();

解决方案

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

Because parsing of json response and downloadConsumerData method making api request from main UI Thread.

In Volley onResponse method called as callback method on main UI Thread when network request is completed.

For parsing request response in onResponse method use AsyncTask class and call ProgressDialog dismiss method in onPostExecute method which will close ProgressDialog when Volley request, parsing of json data and downloadConsumerData method job is done in background Thread

Start AsyncTask from onResponse method :

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

In doInBackground process JSON data:

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

        return null;
    }

这篇关于凌空ProgressDialog卡/冻结在时间获取大量的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆