简体   繁体   English

尝试使用 volly 将 POST 从 android 发送到节点服务器,但服务器上的 json 正文为空

[英]Trying to send POST from android to node server using volly but the json body is empty on the server

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这是节点js代码

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.正如你在 postRequest 方法中看到的,我在 jsonobject 中放置了“hello as key”和“world as value” 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.编辑-----我用wireshark检查了内容,内容长度为0,所以我似乎没有发送任何内容。 if im understanding this correctly如果我正确理解这一点

You are not sending anything inside of your POST body request, that's why you are getting an empty response at your server-side.您没有在POST正文请求中发送任何内容,这就是为什么您在服务器端收到空响应的原因。

For sending POST parameters, you will need to override getParams() method:要发送POST参数,您需要覆盖getParams()方法:

@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 {

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向django服务器发出基本的volly POST请求时出现com.android.volly.AuthFailureError - com.android.volly.AuthFailureError in making basic volly POST request to a django server 如何将JSon作为BODY发送到Android应用程序的服务器POST请求中? - How do I send JSon as BODY In a POST request to server from an Android application? 如何在Android中使用volly将服务器中的单个图像放入我的imageview中? - how to put single image from server into my imageview using volly in android? Android 将带有表情符号的 JSON 正文发送到 nodejs 服务器 - Android send JSON body with emojis to a nodejs server 使用volly将数组作为json在参数中发布 - Post Array as json in parameter using volly 如何通过分段请求中的Volly-Library向服务器发送空参数? - How to send Empty Parameter to Server via Volly-Library in Multipart Request? 使用 POST 方法和 HttpURLConnection 将 JSON 对象从 Android 发送到 PHP 服务器 - Send a JSON Object from Android to PHP server with POST method and HttpURLConnection Android-服务器中的JSON为空 - Android - JSON from server is empty 将JSON的POST请求从Android发送到node.js服务器 - Sending a POST request with JSON from Android to a node.js server 使用POST从Android客户端将数据发送到Java服务器并获取JSON响应 - Send data using POST to java server from Android client and get JSON response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM