0

I'm calling a restful api on my android project and I used Volley and JsonObjectRequest, I thought that the third parameter of the JsonObjectRequest which is jsonRequest are the api parameters so I created a json object for that which in the end I only got errors. So is it common to directly add the api parameters on the url? instead of passing it on a json object? what is the third parameter for, it would be really helpful if someone can give me an example. And my last question is how do you get the entire json response instead of using response.getString("title") for each key.

//api parameters directly added on the url
String URL = "https://www.myapi.com/?param=sample&param1=sample1";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL, null,
            new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    String title = response.getString("Title");
                    Log.d("title", title);
                } catch(Exception e){
                    Log.e("response error", e.toString());
                }

            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.toString());
            }
        });
Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45

1 Answers1

1

Below your Response.ErrorListener() you need to add these two overrides:

new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, error.toString());
    }) {
    @Override
    protected Map<String, String> getParams() 
    {  
            Map<String, String>  params = new HashMap<String, String>();  
            params.put("param", "sample");
            params.put("param1", "sample1);

            return params;  
    }

   @Override
   public Map<String, String> getHeaders() throws AuthFailureError {
       HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/x-www-form-urlencoded; charset=UTF-8");
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
       return headers;
   }
};
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13
  • thanks, but what is the third parameter of the JsonObject, why is it always null? – Kevin Bryan May 23 '19 at 16:19
  • com.android.volley.ParseError: org.json.JSONException: Value – Kevin Bryan May 23 '19 at 16:34
  • The third parameter is not always NULL. The third parameter is for putting a JSON Object that you might want to post to your URL. Say you have a class object, and you convert it to JSON and load it into a JSONObject, then the third parameter would be that object. It's basically putting that data into the message body of the post. On what instruction are you getting the JSON exception? – Michael Dougan May 23 '19 at 19:20
  • I switched to StringRequest and I'm getting an html repose instead of json. I think it's not recognizing the params – Kevin Bryan May 24 '19 at 14:39
  • You can try changing the Accept header to something else. I used that because that was the format that my API was sending back. You need to keep Content-Type as above, because that is what you are sending. Check what is in the HTML. Sometimes errors are returned as HTML such as 500 error, or 400 Permission errors. In my case, I also used a StringRequest, and the response string was actually JSON which I could convert to a JSONObject. – Michael Dougan May 24 '19 at 15:43