2017-04-24 61 views
1

我正在嘗試使用Android Volley的KairosAPI's enroll POST request。但是,我不斷收到錯誤1002,圖像缺少一個或多個所需的參數。我已經嘗試了兩種方法將參數添加到JSON的正文中,我已在代碼中對其進行了概述。缺少參數使用Android Volley POST的Kairos API

這是我的代碼 -

public class MainActivity extends AppCompatActivity { 

    RequestQueue requestQueue; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     postRequestToEnrollPersonInGallery(); 
    } 

    public void postRequestToEnrollPersonInGallery() { 

     final String appId = "3e12****"; 
     final String appKey = "156e06fd782a3304f085f***********"; 
     String mainUrl = "https://api.kairos.com/"; 
     String enrollRequestUrl = "enroll"; 

     requestQueue = Volley.newRequestQueue(this); 

     StringRequest stringRequest = new StringRequest(Request.Method.POST, mainUrl + enrollRequestUrl, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       Log.d("Volley", response); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("Volley", error.toString()); 
      } 
     }) { 
      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String, String> params = new HashMap<>(); 
       params.put("Content-Type", "application/json"); 
       params.put("app_id", appId); 
       params.put("app_key", appKey); 
       return params; 
      } 

      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("image", "https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg"); 
       params.put("subject_id", "12345"); 
       params.put("gallery_name", "FirstGallery"); 
       /*params.put("image", "\"url\":\"https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg\""); 
       params.put("subject_id", "\"subject_id\":\"12345\""); 
       params.put("gallery_name", "\"gallery_name\":\"FirstGallery\""); -- i tried this too*/ 

       return params; 
      } 
     }; 
     requestQueue.add(stringRequest); 
    } 
} 

回答

0

您還沒有張貼JSON。

您可以

1)學會使用JsonObjectRequest

final JSONObject body = new JSONObject(); 
body.put(... , ...); 
Request request = new JsonObjectRequest(url, body, ...); 

2)其實張貼JSON字符串。

StringRequest request = new StringRequest(...) { 
    @Override 
    public byte[] getBody() throws AuthFailureError { 
     JSONObject params = new JSONObject(); 
     params.put("image", "https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg"); 
     params.put("subject_id", "12345"); 
     params.put("gallery_name", "FirstGallery"); 
     return params.toString().getBytes(); 
    } 

    @Override 
    public String getBodyContentType() { 
     return "application/json"; 
    } 
};