2017-02-15 78 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