缺少参数Kairos API与Android Volley POST

时间:2017-04-24 01:18:41

标签: android android-volley kairos-api

我尝试使用Android Volley来KairosAPI's enroll POST request。但是我一直收到错误1002,图像缺少一个或多个必需参数。我已经尝试了两种方法将参数添加到JSON的主体中,我在代码中已经概述了这些。

这是我的代码 -

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);
    }
}

1 个答案:

答案 0 :(得分: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";
    }
};