1

I'm trying to use KairosAPI's enroll POST request using Android Volley. However I keep getting Error 1002, image one or more required parameters are missing. I've tried two ways to add the parameters into the body of the JSON, which I've outlined in the code.

This is my code-

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

1 回答 1

0

You aren't posting JSON.

You can either

1) Learn to use JsonObjectRequest

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

2) Actually post a JSON String.

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";
    }
};
于 2017-04-24T01:23:40.510 回答