2017-05-03 87 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请求?

回答