凌空发送多个Android请求

时间:2017-02-03 17:27:14

标签: android android-volley

我正在向服务器发送请求,这会导致错误。我能够将错误跟踪到Volley不止一次发送请求的事实。我搜索了互联网上的解决方案,我尝试了所有我遇到的但是他们似乎都没有解决问题

以下是我的代码:

 public void btnLogOut(View view) {

    final ProgressDialog loading = ProgressDialog.show(this, "Logging Out", "Please wait...", false, false);
    //cover.setVisibility(View.GONE);
    String token = dbHelper.getAuth().getString(0);

    String IP = helperFunctions.getAppUrl();
    final String url = IP + "/deregister?token=" + token+ "&appVersion=" + versionCode;

    JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, url,null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            loading.dismiss();
            logOut.LogOutUser();
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    if (VolleyErrorHelper.getMessage(error, Settings.this).equalsIgnoreCase("401")){
                        logOut.MakeUserLogin();
                    }else{
                        cover.setVisibility(View.VISIBLE);
                    }
                    //VolleyLog.e("Deregister GCM", "Error: " + error.getMessage());
                    loading.dismiss();
                    Toast.makeText(Settings.this, "Process not completed, try again!", Toast.LENGTH_LONG).show();;
                }
            });
    sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(sr);
}

1 个答案:

答案 0 :(得分:0)

试试这个:

        sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_TIMEOUT_MS));

我用过POST请求,所以可以重试