从 POSTman 发送 POST 请求与从 Android (Java) 发送 POST 请求不同吗?

时间:2020-12-22 11:58:33

标签: javascript android express

我正在尝试从 Android 向我的 Node.js 应用程序发送一个简单的 POST 请求。我正在使用 Volley 发送请求。这是 Android Side 的代码。

 StringRequest stringRequest=new StringRequest(Request.Method.POST, constants.link,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
        }
    })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params=new HashMap<>();
            params.put("number","my-phone-number");
            params.put("text","Hello World");
            return params;
        }
    };
    RequestQueue requestQueue= Volley.newRequestQueue(menu.this);
    requestQueue.add(stringRequest);

现在,当我使用此代码向 PHP Web API 发送 POST 请求时,它工作正常。但是当我使用此代码访问我的 NODE.js 应用程序的端点时。它确实发送请求,但它没有发送参数。

这是我的 Node.js 应用程序的代码:

var app=express();
var bodyParser=require('body-parser');
const port = 3001
var telnyx = require('telnyx')(MY_API_KEY);
var count=0;
var access=0;


app.use(bodyParser.json());

app.get("/app",function(req,res,next){
    //console.log(req);
    console.log(req.query.number);
    console.log(req.query.text);
    res.send("I am get method"+access);
    access++;

});

app.post("/app",function(req,res,next){
  //  console.log(req);
  
   sendSMS(req.query.number,req.query.text);
    res.send("I am post method "+ access + " " + req.query.number + " " + req.query.text);
    access++;
});


app.listen(port,function(){
console.log("Started on port "+port);

});



function sendSMS(num,text) {
console.log("inside function"+num+"  "+text);
    telnyx.messages.create(
      {
        'from': '+15074077011', // Your Telnyx number
        'to': '+'+ num,
        'text': text
      },
      function (err, response) {
        // asynchronously called
        // console.log(response);
        console.log("message sent  "+text+" "+num);
        count++
  
      }
    );
  }```

   
Now when I send a POST request from POSTMAN to this NODE.js application it works fine and I get a message on my Mobile Phone. But when I try to send a post request from android(code is given above), the request sends and the response comes but the parameters stay empty.

1). Is the POST request coming from POSTMAN different from that of Volley from Android
2). How can I send a POST request from Android with parameters that I can catch in my NODE.js application properly?


1 个答案:

答案 0 :(得分:0)

app.use(bodyParser.json());

需要在请求的 BODY 字段中发送的参数,而不是作为 URL 参数。

您发送的请求类似于“../app?number=123&text=message”,而您必须这样做:

JSONObject bodyParams = new JSONObject();
bodyParams.put("number", "1234");
bodyParams.put("text", "message");
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, paramJson,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
requestQueue.add(jsonRequest);