2017-02-15 94 views
0

enter image description hereenter image description here从两天我试图解决这个问题,但我仍然没有任何结果,为什么每次都凌空回国403错误。我错在哪里?我使用邮递员检查相同的webservice,它返回成功结果。但同样的事情,当我通过volley或httpurlconnection得到403 error.kindly帮助我找到我的错误在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); 
+0

403将被禁止。你能否在这里复制邮递员请求(修改任何应该是私人的东西!)。你可以,在邮递员单击代码链接并复制HTTP ... –

+0

@Redman我的用户名和密码是正确的,因为我在邮递员也使用相同的用户名和密码also.postman返回成功的结果。 – Rims

+0

你应该检查base64编码的标记是否符合你的邮递员请求和应用程序请求 – akash93

回答

0

排球确实提供了这个被称为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); 
+0

非常不好主意!!! – Rims

+0

@ReemaSingh你为什么这么说? – Enzokie