post请求参数未获取

2024-06-01 10:34:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从android向python服务器发送POST请求(OKHttp)。在

安卓

public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
String params="name="+Name+"&pass="+Pass;
RequestBody body = RequestBody.create(JSON, params);
Request request=new Request.Builder().url(URL).post(body).build(); //Request{method=POST, url=http://10.XX.XXX.XXX:8080/register, tag=null}
Response res=client.newCall(request).execute();

python服务器

^{pr2}$

为什么标签是空的,我是不是在设置POST参数时漏掉了一点?在


Tags: jsonurlrequeststaticbodyparamspublicpost
1条回答
网友
1楼 · 发布于 2024-06-01 10:34:39

参数不是JSON格式。尝试按如下方式构建参数:

JSONObject paramsJson = new JSONObject();
object.put("name", Name);
object.put("pass", Pass);

String params = object.toString();

如果发生JSONException,还必须使用try/catch。在

相关问题 更多 >