Android Volley里面for循环显示进度

时间:2022-10-22 22:53:14

I need to show a loading screen for volley, I am sending few json to the servers using volley, i written the code inside for loop for fetching a json and sending the json. But the for loop is not waiting for the responce to continue. It just completing quickly, How can i use for loop for sending jason and show the for loops value as the count of syncing.?

我需要显示一个凌空的加载屏幕,我使用齐射将少量json发送到服务器,我在内部编写代码以获取循环以获取json并发送json。但for循环不等待响应继续。它只是快速完成,我如何使用for循环发送jason并显示for循环值作为同步计数。?

Code...

码...

 @Override
    protected void onPostExecute(JSONArray result) {
        super.onPostExecute(result);

        Log.d("length", String.valueOf(result.length()));
        if(result.length()!=0) {
            for (int e = 0; e < result.length(); e++) {
                this.dialog.setMessage("Syncing "+(e+1)+" of "+result.length());
                this.dialog.show();
                try {
                    final JSONObject js = result.getJSONObject(e);
                    Log.d("sdfhd", js.toString());
                    final JSONArray ja = new JSONArray();
                    ja.put(js);
                    Log.d("sending json", ja.toString());
                    JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST,
                            server_url, ja,
                            new Response.Listener<JSONArray>() {

                                @Override
                                public void onResponse(JSONArray response) {
                                    Log.d("asd", ja.toString());
                                    Log.d("JsonArray", response.toString());
                                    for (int i = 0; i < response.length(); i++) {

                                        try {
                                            JSONObject store = response.getJSONObject(i);
                                            mydb.updateweb(store.getInt("_id"), store.getString("webid"), store.getString("regid"));

                                            if (i == response.length() - 1) {
                                                Toast.makeText(MainActivity.this, "Synced Successfully", Toast.LENGTH_SHORT).show();

                                                mydb.updatesync();
                                                mydb.deletet2();
                                                dialog.dismiss();
                                            }

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }

                                    try {
                                        JSONObject stor = response.getJSONObject(0);
                                        Toast.makeText(MainActivity.this, stor.getString("result"),
                                                Toast.LENGTH_SHORT).show();

                                        mydb.updatesync();
                                        mydb.deletet2();
                                       // dialog.dismiss();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }


                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {

                                    Log.d("Json sdfsdfsf", js.toString());
                                    Log.d("afs", error.toString());


                                    Toast.makeText(MainActivity.this, "error",
                                            Toast.LENGTH_SHORT).show();
                                    dialog.dismiss();


                                }
                            }) {

                        @Override
                        public String getBodyContentType() {
                            return "application/json";
                        }
                    };
                    request.setRetryPolicy(new DefaultRetryPolicy(50000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                    requestQueue.add(request);
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }

            }
        }
        else{
            Toast.makeText(MainActivity.this, "Updated Succesfully",
                    Toast.LENGTH_SHORT).show();

            mydb.updatesync();
            mydb.deletet2();
            dialog.dismiss();
        }
      // dialog.dismiss();
    }

1 个解决方案

#1


0  

Look at the code I have changed your logic about the dialog, also I have added success and failure counts functionality so that you can have an idea of request statuses

看看我已经改变了关于对话框的逻辑的代码,我还添加了成功和失败计数功能,以便您可以了解请求状态

Global Variables:

全局变量:

int successCount = 0;
int failedCount = 0;

Revised onPostExecute():

修改onPostExecute():

 @Override
protected void onPostExecute(JSONArray result) {
    super.onPostExecute(result);

    successCount = 0;
    failedCount = 0;

    this.dialog.setMessage("Syncing 0 of "+result.length());
    this.dialog.show();

    Log.d("length", String.valueOf(result.length()));
    if(result.length()!=0) {
        for (int e = 0; e < result.length(); e++) {
        this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());
            try {
                final JSONObject js = result.getJSONObject(e);
                Log.d("sdfhd", js.toString());
                final JSONArray ja = new JSONArray();
                ja.put(js);
                Log.d("sending json", ja.toString());
                JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST,
                        server_url, ja,
                        new Response.Listener<JSONArray>() {

                            @Override
                            public void onResponse(JSONArray response) {
                                boolean isCodeExecutedSuccessfully = false;
                                Log.d("asd", ja.toString());
                                Log.d("JsonArray", response.toString());
                                for (int i = 0; i < response.length(); i++) {

                                    try {
                                        JSONObject store = response.getJSONObject(i);
                                        mydb.updateweb(store.getInt("_id"), store.getString("webid"), store.getString("regid"));

                                        if (i == response.length() - 1) {
                                            Toast.makeText(MainActivity.this, "Synced Successfully", Toast.LENGTH_SHORT).show();

                                            mydb.updatesync();
                                            mydb.deletet2();
                                            dialog.dismiss();
                                        }

                                        isCodeExecutedSuccessfully = true;
                                    } catch (JSONException e) {
                                        isCodeExecutedSuccessfully = false;
                                        e.printStackTrace();
                                    }

                                }

                                try {
                                    JSONObject stor = response.getJSONObject(0);
                                    Toast.makeText(MainActivity.this, stor.getString("result"),
                                            Toast.LENGTH_SHORT).show();

                                    mydb.updatesync();
                                    mydb.deletet2();
                                    // dialog.dismiss();
                                    isCodeExecutedSuccessfully = true;

                                } catch (JSONException e) {
                                    isCodeExecutedSuccessfully = false;
                                    e.printStackTrace();
                                }


                                //////////////////////////////// DIALOG WORK ON SUCCESS ///////////////////////////////
                                if(isCodeExecutedSuccessfully)
                                    ++successCount;
                                else
                                    ++failedCount;

                                if((e+1) == result.length()) {
                                    dialog.dismiss();
                                }

                                this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());

                                Log.d("::onResponse(): Status Counts", "Success=" + successCount + ", fail="  + failedCount);
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {

                                //////////////////////////////// DIALOG WORK ON ERROR RESPONSE ///////////////////////////////
                                ++failedCount;
                                if((e+1) == result.length()) {
                                    dialog.dismiss();
                                }
                                Log.d("::onErrorResponse(): Status Counts", "Success=" + successCount + ", fail="  + failedCount);
                                this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());


                                Log.d("Json sdfsdfsf", js.toString());
                                Log.d("afs", error.toString());


                                Toast.makeText(MainActivity.this, "error",
                                        Toast.LENGTH_SHORT).show();
                                //dialog.dismiss();
                            }
                        }) {

                    @Override
                    public String getBodyContentType() {
                        return "application/json";
                    }
                };
                request.setRetryPolicy(new DefaultRetryPolicy(50000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                requestQueue.add(request);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

        }
    }
    else{
        Toast.makeText(MainActivity.this, "Updated Succesfully",
                Toast.LENGTH_SHORT).show();

        mydb.updatesync();
        mydb.deletet2();
        dialog.dismiss();
    }
  // dialog.dismiss();
}

#1


0  

Look at the code I have changed your logic about the dialog, also I have added success and failure counts functionality so that you can have an idea of request statuses

看看我已经改变了关于对话框的逻辑的代码,我还添加了成功和失败计数功能,以便您可以了解请求状态

Global Variables:

全局变量:

int successCount = 0;
int failedCount = 0;

Revised onPostExecute():

修改onPostExecute():

 @Override
protected void onPostExecute(JSONArray result) {
    super.onPostExecute(result);

    successCount = 0;
    failedCount = 0;

    this.dialog.setMessage("Syncing 0 of "+result.length());
    this.dialog.show();

    Log.d("length", String.valueOf(result.length()));
    if(result.length()!=0) {
        for (int e = 0; e < result.length(); e++) {
        this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());
            try {
                final JSONObject js = result.getJSONObject(e);
                Log.d("sdfhd", js.toString());
                final JSONArray ja = new JSONArray();
                ja.put(js);
                Log.d("sending json", ja.toString());
                JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST,
                        server_url, ja,
                        new Response.Listener<JSONArray>() {

                            @Override
                            public void onResponse(JSONArray response) {
                                boolean isCodeExecutedSuccessfully = false;
                                Log.d("asd", ja.toString());
                                Log.d("JsonArray", response.toString());
                                for (int i = 0; i < response.length(); i++) {

                                    try {
                                        JSONObject store = response.getJSONObject(i);
                                        mydb.updateweb(store.getInt("_id"), store.getString("webid"), store.getString("regid"));

                                        if (i == response.length() - 1) {
                                            Toast.makeText(MainActivity.this, "Synced Successfully", Toast.LENGTH_SHORT).show();

                                            mydb.updatesync();
                                            mydb.deletet2();
                                            dialog.dismiss();
                                        }

                                        isCodeExecutedSuccessfully = true;
                                    } catch (JSONException e) {
                                        isCodeExecutedSuccessfully = false;
                                        e.printStackTrace();
                                    }

                                }

                                try {
                                    JSONObject stor = response.getJSONObject(0);
                                    Toast.makeText(MainActivity.this, stor.getString("result"),
                                            Toast.LENGTH_SHORT).show();

                                    mydb.updatesync();
                                    mydb.deletet2();
                                    // dialog.dismiss();
                                    isCodeExecutedSuccessfully = true;

                                } catch (JSONException e) {
                                    isCodeExecutedSuccessfully = false;
                                    e.printStackTrace();
                                }


                                //////////////////////////////// DIALOG WORK ON SUCCESS ///////////////////////////////
                                if(isCodeExecutedSuccessfully)
                                    ++successCount;
                                else
                                    ++failedCount;

                                if((e+1) == result.length()) {
                                    dialog.dismiss();
                                }

                                this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());

                                Log.d("::onResponse(): Status Counts", "Success=" + successCount + ", fail="  + failedCount);
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {

                                //////////////////////////////// DIALOG WORK ON ERROR RESPONSE ///////////////////////////////
                                ++failedCount;
                                if((e+1) == result.length()) {
                                    dialog.dismiss();
                                }
                                Log.d("::onErrorResponse(): Status Counts", "Success=" + successCount + ", fail="  + failedCount);
                                this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());


                                Log.d("Json sdfsdfsf", js.toString());
                                Log.d("afs", error.toString());


                                Toast.makeText(MainActivity.this, "error",
                                        Toast.LENGTH_SHORT).show();
                                //dialog.dismiss();
                            }
                        }) {

                    @Override
                    public String getBodyContentType() {
                        return "application/json";
                    }
                };
                request.setRetryPolicy(new DefaultRetryPolicy(50000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                requestQueue.add(request);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

        }
    }
    else{
        Toast.makeText(MainActivity.this, "Updated Succesfully",
                Toast.LENGTH_SHORT).show();

        mydb.updatesync();
        mydb.deletet2();
        dialog.dismiss();
    }
  // dialog.dismiss();
}