【问题标题】:Volley return 403 errorVolley 返回 403 错误
【发布时间】:2017-02-15 05:21:16
【问题描述】:

从两天开始我试图解决这个问题,但我仍然没有任何结果,为什么每次凌空返回我 403 错误。我错在哪里?我正在使用邮递员检查相同的网络服务,它返回成功结果。但是当我通过 volley 或 httpurlconnection 在 Android 中使用时,同样的事情会出现 403 错误。请帮助我找到我的错误。

这是我尝试过的代码:

  StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, Constant.posturl, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                      String result=response;
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        NetworkResponse response = error.networkResponse;
                        int status = response.statusCode;

                    }
                }) {
                    @Override
                    public Map<String, String> getHeaders() {
                        try {
                              headers = new HashMap<String, String>();
                              headers.put("Content-Type", "application/json");
                              String credentials = Constant.USERNAME + ":" + Constant.PASSWORD;
                              String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.DEFAULT);
                              headers.put("Authorization", auth);
                              return headers;
                        } catch (Exception e) {
                            e.printStackTrace();
                             return headers;
                        }
                    }
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("title", heading_edit_text.getText().toString());
                        params.put("content", body_edit_text.getText().toString());
                        params.put("Slug", heading_edit_text.getText().toString());
                        params.put("date", currentDate);
                        return params;
                    }
                };
                jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(50000, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                requestQueue.add(jsonObjectRequest);

【问题讨论】:

  • 403 将被禁止。你能在这里复制你的邮递员请求吗(编辑任何应该是私人的!)。您可以在邮递员中单击代码链接并复制 HTTP...
  • @Redman 我的用户名和密码是正确的,因为我在邮递员中也使用了相同的用户名和密码。邮递员返回成功结果。
  • 您应该检查 base64 编码的令牌是否与您的邮递员请求和应用程序请求匹配
  • 是的,它是匹配的,甚至有时我也通过在应用程序中使用我在邮递员中使用的相同字符串来检查它。
  • @Gavin Harris 我用邮递员图像编辑了我的问题...请检查它并让我知道我在应用程序中做错了什么。

标签: android android-volley


【解决方案1】:

Volley 确实为此提供了一个适当的请求,称为 JsonObjectRequest

 String webAddress = "url here";
 RequestQueue queue = Volley.newRequestQueue(this); // singletone here

 JSONObject object = new JSONObject();
 try {
     object.put("title", heading_edit_text.getText().toString());
     object.put("content", body_edit_text.getText().toString());
     object.put("Slug", heading_edit_text.getText().toString());
     object.put("date", currentDate);
 } catch (JSONException e) {
 }

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, webAddress,object, new Response.Listener<JSONObject>() {

     @Override
     public void onResponse(JSONObject object) {
         Log.d("RESPONSE", object.toString());
     }

 }, new Response.ErrorListener() {

     @Override
     public void onErrorResponse(VolleyError volleyError) {
         Log.d("RESPONSE", "That didn't work!");
     }

 }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> header = new HashMap<>();
                // content type is not needed here
                header.put("Authorization", "value here");
                return header;
            }

  };
 queue.add(request); 

【讨论】:

  • @ReemaSingh 你为什么这么说?
【解决方案2】:

将标题的“Content-Type”更改为“application/form-data”

即,

headers.put("Content-Type", "application/form-data");

【讨论】:

    【解决方案3】:

    我在新闻 api 中也遇到过这个问题。但是当我使用 Retrfit 时,它的工作就像魅力一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-06
      • 2013-01-02
      • 1970-01-01
      • 2011-01-26
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多