繁体   English   中英

无法使用凌空发送POST JsonObjectRequest

[英]Can't send POST JsonObjectRequest using volley

我只想使用截击发送POST请求,但在服务器端却什么也没得到:

这是我的代码:

安卓

JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, URL,null,
                    new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response){

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

                }
            }){
                @Override
                protected Map<String, String> getParams() throws 
         AuthFailureError {
                    Map<String,String> params = new HashMap<>();
                    params.put("email",email.getText().toString());
                    return params;
                }
            };
            queue.getInstance(getApplicationContext()).add(sr);

节点

const express = require('express');
const app = express() ; 
const bodyParser = require('body-parser') ; 


app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json()) ; 

app.post('/register',(req,res)=>{
//email and passwords

console.log(req.body);
})

app.listen(80);

结果如下: {}

但是,当我使用StringRequest一切正常,但是当我使用JsonObjectRequest node.js似乎无法读取传入请求的主体。

您将为JsonObjectRequest的第三个参数传递null。

“允许为空,表示不随请求一起发布任何参数。” https://android.googlesource.com/platform/frameworks/volley/+/43950676303ff68b23a8b469d6a534ccd1e08cfc/src/com/android/volley/toolbox/JsonObjectRequest.java#40

如果要使用JsonObjectRequest,请在此处传递JSONObject。

Map<String,String> params = new HashMap<>();
params.put("email", "email@email.com");
JSONObject json = new JSONObject(params);

JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, URL, json, ...)
queue.getInstance(getApplicationContext()).add(sr);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM