0
votes

I am looking to do as per the image says:

Kairos SDK Request

Following is the code I am trying to implement from that image:

RequestQueue queue = Volley.newRequestQueue(this); String url ="https://api.kairos.com/enroll";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        Log.i("Response is: " , response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
               // mTextView.setText("That didn't work!");
            }
        })
        {

            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String> params = new HashMap<String, String>();
                params.put("app_id", "4985f625");
                params.put("app_key", "aa9e5d2ec3b00306b2d9588c3a25d68e");

                return params;
            }
        };
        // Add the request to the RequestQueue.
        queue.add(stringRequest);

Now I do not get how to add that JSONObject part into my POST Request, and also how to add the Content-Type Header. Please help me out on this.

2

2 Answers

0
votes

I found a similar question here. See the code below. You have to override the getBodyContentType method.

public String getBodyContentType()
{
    return "application/json";
}
0
votes

for content type header you can do the following

StringRequest request = new StringRequest(Request.Method.PUT,
            url, new Response.Listener<String>() {
      @Override
      public void onResponse(String response) {
        listener.onResponse(response);
      }
    }, new Response.ErrorListener() {
      @Override
      public void onErrorResponse(@NonNull VolleyError error) {
        if (error.networkResponse != null) {
          errorListener.onErrorResponse(error.networkResponse.statusCode, null);
        } else {
          Log.e(TAG, "An error occurred while trying to verify sms: ", error);
          errorListener.onErrorResponse(500, null);
        }
      }
    }) {
      @NonNull
      @Override
      protected Map<String, String> getParams() {
        return data;
      }

      @NonNull
      @Override
      public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type",
                "application/x-www-form-urlencoded");
        return headers;
      }
    };

And for send Json object I suggest create Json object like this

JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("11", 3);
            jsonObject.put("12", 4);
            jsonObject.put("13", 5);
        } catch (JSONException e) {
            e.printStackTrace();
        }

Then you can pass this object as string by jsonObject.toString() and pass it in parameters like pass any string like the following

@NonNull
          @Override
          protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("json", jsonObject.toString();
            return params;
          }