1

I am creating an app which uses some node.js scripts for server scripting mainly because node.js has Firebase support, but for some reason I am unable to send any params with my request (both GET and POST).

My android code is as follows

private void sendData(final String name){
    StringRequest sr = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Toast.makeText(getApplicationContext(),response.toString()+" returned from node.js server",Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),error.toString()+"The Server returned error",Toast.LENGTH_SHORT).show();

        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String,String> map = new HashMap<>();
            map.put("test",name);
            return map;
        }
    };
    RequestQueue r = Volley.newRequestQueue(getApplicationContext());
    r.add(sr);
}

The corresponding node.js script is as follows

app.get('/hello', function(request, response){
  var outputJson = request.params.test;
  response.send(JSON.stringify(outputJson));
  console.log(outputJson);
})

The console always logs undefined for some reason. Following suggestions from this post I also tried including the data in the body of the request and get the data from the request in the node.js via request.body.test.

I would like to know what is going wrong and most importantly I would also like to know how to recieve and process POST request. I read on some blog that I have to implement a middleware of some kind. Some clarification in that area will also be appreciated.

Community
  • 1
  • 1
Tyson
  • 747
  • 2
  • 6
  • 18

1 Answers1

2

I have looked at your code and found that there are some flaws in the Android Code.

  1. The HashMap is defined in a getHeaders() function but it should actually be defined in getParams() like this

     @Override
                public Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String,String> map = new HashMap<>();
                    map.put("test",name);
                    return map;
                }
    
  2. Instead of trying to retrieve the data by request.params try using request.body.
Nirmal Raj
  • 729
  • 6
  • 18
  • by doing a request.body in node, he needs to override the getBody() in his android code if that is the case since if not, that would throw an undefined object property, request.params will be enough I believed – Roljhon Jan 16 '17 at 14:42
  • I too thought of it. That would have been my second guess if the post was not accepted. But apparently it got solved – Nirmal Raj Jan 16 '17 at 14:44
  • the answer wasn't accepted yet, anyway, your answers is still helpful. :) – Roljhon Jan 16 '17 at 16:24
  • Oops. I got a reputation change and assumed it was due to this question. Thanks for bringing that to my attention. – Nirmal Raj Jan 16 '17 at 16:25