在两个或多个android stringRequest Volley中使用一个progressDialog

时间:2016-04-20 09:53:47

标签: android-volley progressdialog

在我的代码中,我必须stringRequest Volley工作正常,但现在我想使用progressDialog。我创建了一个方法来放置progressDialog这样的

private void showProgress(String message) {
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Loading Data " + message);
    progressDialog.setMessage("Please wait...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setCancelable(false);
    progressDialog.show();
}

我有这样的stringRequest这样:

private void fetchDataPoMurni(final String tipe, final String user_id, final String last_date) {
    showProgress("Murni");
    String tag_string_req = "Request Po Dapat";
    StringRequest stringRequest = new StringRequest(
        Request.Method.POST,
        AppConfig.URL_FETCH_REPORT_PO,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    boolean error = jsonObject.getBoolean("error");

                    if(!error) {
                        JSONArray resultPo =  jsonObject.getJSONArray("result");
                        for(int i = 0; i < resultPo.length(); i++) {
                            JSONObject result = (JSONObject) resultPo.get(i);
                            String _cabang_id = result.getString("branch_id");
                            String _area_id = result.getString("areacode");
                            String _cabang = result.getString("branch_name");
                            Log.d("FETCHING DATA MURNI: ", _cabang_id + " " + _area_id + " " + _cabang);
                            dataBaseHelper.insertDataPoMurni(new PoModel(_cabang_id.trim(), _area_id.trim(), _cabang.trim()));
                        }
                    } else {
                        String errorMsg = jsonObject.getString("result");
                        showAlertDialog(errorMsg);
                    }

                } catch (JSONException e) {
                    showAlertDialog(e.getMessage());
                }

                if (progressDialog != null) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                showAlertDialog(volleyError.getMessage());
                if (progressDialog != null) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("tipe", tipe);
                params.put("uid", user_id);
                params.put("last_date", last_date);
                return params;
            }
        };
    AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
}

和另一个请求:

private void fetchDataPoDapat(final String tipe, final String user_id, final String last_date) {
    showProgress("Dapat");
    String tag_string_req = "Request Po Dapat";
    StringRequest stringRequest = new StringRequest(
        Request.Method.POST,
        AppConfig.URL_FETCH_REPORT_PO,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    boolean error = jsonObject.getBoolean("error");

                    if(!error) {
                        JSONArray resultPo =  jsonObject.getJSONArray("result");
                        for(int i = 0; i < resultPo.length(); i++) {
                            JSONObject result = (JSONObject) resultPo.get(i);
                            String _cabang_id = result.getString("branch_id");
                            String _area_id = result.getString("areacode");
                            String _cabang = result.getString("branch_name");
                            Log.d("FETCHING DATA DAPAT : ", _cabang_id + " " + _area_id + " " + _cabang);
                            dataBaseHelper.insertDataPoDapat(new PoModel(_cabang_id.trim(), _area_id.trim(), _cabang.trim()));
                        }
                    } else {
                        String errorMsg = jsonObject.getString("result");
                        showAlertDialog(errorMsg);
                    }
                } catch (JSONException e) {
                    showAlertDialog(e.getMessage());
                }
                if (progressDialog != null) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                showAlertDialog(volleyError.getMessage());
                if (progressDialog != null) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("tipe", tipe);
                params.put("uid", user_id);
                params.put("last_date", last_date);
                return params;
            }
        };
    AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
}

我通过以下方法执行2请求:

private void exeRequest() {
    fetchDataPoMurni(valuea,value2,value3);
    fetchDataPoDapat(valueb,value2,value3);
}

progressDialog正在显示,并且消息正在更改,但问题是当达到progressDialog不想解雇的第二个请求时。

上面的代码有什么问题,以及如何实现我的目标?

1 个答案:

答案 0 :(得分:0)

private void showProgress(String message) {
  progressDialog=null;// Initialize to null 
   progressDialog = new ProgressDialog(this);
      progressDialog.setTitle("Loading Data " + message);
    progressDialog.setMessage("Please wait...");
   progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
   progressDialog.setCancelable(false);
   progressDialog.show();
 }

试试这个..一旦创建新的进度对话框,就将progressDialog的所有实例初始化为null