2017-05-03 114 views
-1

我想用戶的Android的凌空庫效仿,下列curl請求,請求:凌空GET與JSON數據

curl -H "Content-Type: application/json" -X GET -d '{"password":"password"}' https://0.0.0.0:5000/staging/api/v2.0/supporter/name=Steven 

預期其作品,但下面的代碼不針對Android工作:

public void getSupporterByName(String name, String password, @NonNull final APIRequestCallback<Supporter> callback){ 
    try { 
     JSONObject job = new JSONObject(); 
     job.put("password", password); 

     JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, getAPIEndpoint() + "supporter/name=" + name, job, new Response.Listener<JSONObject>() { 
      @Override public void onResponse(JSONObject response) { 
       callback.onSuccess(parseSupporter(response.toString())); 
      } 
     }, new Response.ErrorListener() { 
      @Override public void onErrorResponse(VolleyError error) { 
       callback.onFailure(error); 
      } 
     }) { 
      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String, String> currs = super.getHeaders(); 
       Map<String, String> map = currs != null ? new HashMap<>(super.getHeaders()) : new HashMap<String, String>(); 
       map.put("Content-Type", "application/json"); 
       return map; 
      } 
     }; 
     requestQueue.add(req); 
    } catch (Exception ex) { 
     callback.onFailure(ex); 
    } 
} 

我不斷從API獲取400個錯誤的請求,我知道這是因爲API沒有看到發送的JSONObject,因此無法驗證密碼。有沒有辦法通過使用Volley的JSON主體發出GET請求?

回答