排球:请求被发送两次

时间:2017-01-05 19:32:09

标签: android android-volley

我知道在这个网站上有类似的问题,我已经尝试了一些建议解决方案,其中一些解决方案适用于之前提出这个问题的人。但是,我仍然收到两次而不是一次发送的相同数据 这是代码:

final ProgressDialog progressDialog = DialogsUtils.showProgressDialog(DirectSendActivity.this, getResources().getString(R.string.sending));
    new android.os.Handler().postDelayed(
            new Runnable() {
                public void run() {
                    StringRequest stringRequest = new StringRequest(Request.Method.POST,
                            direct_send_url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonResponse = new JSONObject(response);
                                boolean succcess = jsonResponse.getBoolean("success");
                                if (succcess) {
                                    onSendSuccess();
                                    progressDialog.dismiss();
                                } else {
                                    onSendFailed();
                                    progressDialog.dismiss();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            NetworkResponse networkResponse = error.networkResponse;
                            Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse);
                            Toast.makeText(getApplicationContext(), "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                            Snackbar.make(findViewById(android.R.id.content), getResources().getString(R.string.connection_problem) , Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                        }
                    }){
                        @NonNull
                        @Override
                        protected Map<String, String> getParams() {
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("method",method);
                            params.put("amount", a_mount + "");
                            params.put("recipient",recipient);
                            params.put("reason",reason);
                            params.put("user_id",user_data.getString("uidkey", null));
                            Log.e(TAG, "Params: " + params.toString());
                            return params;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    MyApplication.getInstance().addToRequestQueue(stringRequest);
                }
            }, 3000);

1 个答案:

答案 0 :(得分:0)

只需在将请求添加到请求队列之前添加它

request.setRetryPolicy(new DefaultRetryPolicy(
                   0,
                   DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                   DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));