如何处理具有并行多个Volley请求的单个progressdialog

时间:2016-08-19 06:55:46

标签: android android-fragments android-asynctask android-volley progressdialog

我正在使用tablayout包含两个选项卡。每次呼叫截击请求。我希望在第一个请求启动时显示进度对话框,并在上一个请求完成后关闭对话框。

1 个答案:

答案 0 :(得分:-1)

使用AsyncTask,它有PreExecute在后台作业之前启动任务,PostExecute在后台作业之后执行某项任务。

在我的示例中,我在preexecute中执行了一个进度对话框并启动了。在完成整个doinbackground排球请求后,它将在postexecute中被解雇。

private class initoffline extends AsyncTask<String, String, String> {

        private String resp;
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(SplashActivity.this);
            pDialog.setMessage("First Initializing...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {

            StringRequest stringRequest = new StringRequest(URL1,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            SharedPreferences pref = getApplicationContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("OFFLINEPROJECT", response);
                            editor.apply();
                            Log.e("Req 1", response);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
        StringRequest stringRequest1 = new StringRequest(URL2",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        SharedPreferences pref = getApplicationContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = pref.edit();
                        editor.putString("OFFLINEFLATNAMES", response);
                        editor.apply();
                        Log.e("Req 2", response);
                    }
                },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
            StringRequest stringRequest2 = new StringRequest(URL3,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            SharedPreferences pref = getApplicationContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("OFFLINEIMAGE", response);
                            editor.apply();
                            Log.e("Req 3", response);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
            StringRequest stringRequest3 = new StringRequest(URL4,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            SharedPreferences pref = getApplicationContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("OFFLINEVIDEO", response);
                            editor.apply();
                            Log.e("Req 4", response);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            requestQueue.add(stringRequest);
            requestQueue.add(stringRequest1);
            requestQueue.add(stringRequest2);
            requestQueue.add(stringRequest3);


            return resp;
        }

        @Override
        protected void onPostExecute(String result) {
            pDialog.dismiss();


        }
    }