0
private void postRequest() {
    // Request a string response from the provided URL.
    // Instantiate the RequestQueue.
    String url = "http://10.0.0.9:3000/hello";

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.POST, url,  null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try{
                        Log.i("*********", "******");
                        response.put("Hello", "World");
                    }catch(JSONException e){
                        Log.i("JSONERROR", e.toString());
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error
                    Log.i("*********", error.toString());
                }
            });
    jsonObjectRequest.setRetryPolicy(new RetryPolicy() {
        @Override
        public int getCurrentTimeout() {
            return 1000;
        }

        @Override
        public int getCurrentRetryCount() {
            return 1000;
        }

        @Override
        public void retry(VolleyError error) throws VolleyError {

        }
    });

This is the Android code

app.post('/hello', function(req,res){
console.log(JSON.stringify(req.body))
})

This is the node js code

So the problem im having is that im printing the body to the console but it keeps showing up empty. As you can see in the postRequest method ive put 'hello as key' and 'world as value' in the jsonobject. So it is calling the correct post request, the body is just empty and cant figure out why that is.

Edit----- Ive checked the content with wireshark and content length is 0 so im not sending anything it seems. if im understanding this correctly

1 Answers1

1

You are not sending anything inside of your POST body request, that's why you are getting an empty response at your server-side.

For sending POST parameters, you will need to override getParams() method:

@Override
protected Map<String, String> getParams() {
     Map<String, String> map = new HashMap<String, String>();
     map.put("param1", "hello");
     map.put("param2", "This is a post request");
     return map;
}

Here is the complete example:

private void postRequest() {
    // Request a string response from the provided URL.
    // Instantiate the RequestQueue.
    String url = "http://10.0.0.9:3000/hello";

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.POST, url,  null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try{
                        Log.i("*********", "******");
                        response.put("Hello", "World");
                    }catch(JSONException e){
                        Log.i("JSONERROR", e.toString());
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error
                    Log.i("*********", error.toString());
                }
            }) { // HERE TO ADD POST PARAMETERS
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<String, String>();
                map.put("param1", "hello");
                map.put("param2", "This is a post request");
                return map;
            }
        };
    jsonObjectRequest.setRetryPolicy(new RetryPolicy() {
        @Override
        public int getCurrentTimeout() {
            return 1000;
        }

        @Override
        public int getCurrentRetryCount() {
            return 1000;
        }

        @Override
        public void retry(VolleyError error) throws VolleyError {

        }
    });
  • Yes, thanks thats what i just figured out. I followed this guide https://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working/19945676#19945676 – Benjamin Kararic Sep 05 '20 at 19:15