-1

I am trying to call an API through android it is giving invalid request error.

while API giving the correct result in POSTMAN.

I am using the following code . Is something wrong in following code.

 String url ="http://apiaddress";

// POST parameters
 Map<String, String> params = new HashMap<String, String>();
 params.put("tag", "test");

  JSONObject jsonObj = new JSONObject(params);

// Request a json response from the provided URL
  JsonObjectRequest jsonObjRequest = new JsonObjectRequest
  (Request.Method.POST, url, jsonObj, new Response.Listener<JSONObject>()
    {
        @Override
        public void onResponse(JSONObject response)
        {
            System.out.print("JSOn Object:"+jsonObj.toString());
        }
     });
adesh singh
  • 1,727
  • 9
  • 38
  • 70
  • Are you getting any exception ? If yes kindly edit question and post stack trace from logcat as well. If not, kindly check whether that url is accessible from phone or not. – Shadow Droid Dec 19 '19 at 10:23
  • Try to check your permission in manifest like internet and all. – Ankita Dec 19 '19 at 10:29
  • See this https://stackoverflow.com/questions/41935546/getting-response-code-200-in-postman-but-not-on-android-network-library – Ankita Dec 19 '19 at 10:31
  • System.out.print("JSOn Object:"+jsonObj.toString()); in this line it shows invalid request – adesh singh Dec 19 '19 at 10:33

1 Answers1

0

In Volley, JsonObjectRequest seems not to work for POST requests. Use StringRequest instead.

Check this answer, too.

Also, override the getParams method of the StringRequest and return your params as a map.

Bob
  • 13,447
  • 7
  • 35
  • 45