当与Volley一起使用时,Android Progress对话框有时不会关闭

时间:2019-03-09 08:58:40

标签: android android-volley

我正在使用我的android应用程序中的volley进行REST API调用。该代码在大多数情况下都有效。但是,有时进度对话框不会消失,我需要重新启动应用程序。我究竟做错了什么?下面的代码。谢谢

private void getServerCountsForLocation(JSONObject mDataPacket) {

    String url = REST_API_END_POINT + "GetServerCounts/";

    if (myApp.isNetworkAvailable()) {
        progressDialog = ProgressDialog.show(this, "", "Getting total counts", false);

        //call the rest api
        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.POST, url, mDataPacket, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (progressDialog != null && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        parseResponseForServerCounts(response);
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (progressDialog != null && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        renderView();
                    }
                });
        jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(3000, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        VolleySingleton.getInstance(this.getApplicationContext()).getRequestQueue().add(jsObjRequest);
    } else {
        Log.print("Network connection not available in DeviceCountActivity for total counts query");
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        Toast.makeText(DeviceCountActivity.this, "Network connection unavailable. Please try again after connecting with WiFi or SIM.", Toast.LENGTH_LONG).show();
    }
    return;
}

0 个答案:

没有答案