排球:发布请求不起作用:错误意外响应代码404

时间:2018-01-25 13:35:23

标签: android http-headers android-volley restful-url

尝试注册用户但收到错误意外的响应代码404,虽然URL正确,参数也正确,我已经在POSTMAN上测试了它并且它工作正常但是当我尝试提出要求:

  

E / Volley:[19358] BasicNetwork.performRequest:GIVEN URL的意外响应代码404

参数似乎没有附加到网址,但我也提出了自定义请求,但这也不起作用,请帮忙。

注意:唯一的标题是" application/x-www-form-urlencoded";

 private void registerUser()
                {
StringRequest stringRequest = new StringRequest(Request.Method.POST, APIReference.USER_REGISTRATION_URL, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                Toast.makeText(mContext,jsonObject.getString("status"),Toast.LENGTH_LONG).show();
                                showSnackbar(jsonObject.getString("status"));

                                JSONObject object = jsonObject.getJSONObject("response");
                                User user = new User(object.getString("user_id"),
                                        object.getString("user_name"),
                                        object.getString("user_email"),
                            object.getString("user_phone_no"));

                Toast.makeText(mContext,String.valueOf(jsonObject.getJSONObject("response")),Toast.LENGTH_LONG).show();

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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("response error : ", String.valueOf(error));
            showSnackbar(String.valueOf(error));
         }
    })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("user_name", mUserName);
            params.put("user_email", mUserEmail);
            params.put("user_password", mUserPassword);
            params.put("user_phone_no", mUserMobileNo);
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Content-Type", "application/x-www-form-urlencoded,charset=utf-8");
            return params;
        }

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded,charset=utf-8";
        }
    };
    VolleySingleton.getInstance(mContext).getRequestQueue().add(stringRequest);
}

1 个答案:

答案 0 :(得分:-1)

有时JSON网址响应正常,但在Android中,由于超时,它在Volley Library上显示错误。所以你必须设定时间。

在您的Volley Code上添加此代码:

private static final int MY_SOCKET_TIMEOUT_MS = 60000;

 eventoReq.setRetryPolicy(new DefaultRetryPolicy(
            MY_SOCKET_TIMEOUT_MS,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

完整代码:

String url = urlhere;
    Log.e("URL", "" + url);

    StringRequest eventoReq = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("RESPONSE", response.toString());

                    utils.hideDialog();
                    try {
                        JSONArray j = new JSONArray(response);

                         //code here
                                }

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

                        }

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


                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error: ", "" + error.getMessage());
            utils.hideDialog();
            utils.showtoast("Please try again...");

        }
    });
    eventoReq.setRetryPolicy(new DefaultRetryPolicy(
            MY_SOCKET_TIMEOUT_MS,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Añade la peticion a la cola
    AppController.getInstance(this).addToRequestQueue(eventoReq);