我尝试使用以下命令从Android设备向Nodejs服务器发送请求
HttpURLConnection con = (HttpURLConnection) (new URL(IP + "/getrestaurant")).openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
con.getOutputStream().write("{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}".getBytes("UTF-8"));
con.getOutputStream().flush();
con.getOutputStream().close();当我打印从节点js收到的请求时,我得到:
{
"{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}": ""
}代替{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}
我怎么才能让它工作呢?谢谢!
发布于 2016-01-21 18:35:31
正如评论所说(我的声誉太低,无法评论),{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}不是有效的上下文
另外,我建议您使用Koush Ion库来处理json请求。https://github.com/koush/ion
这是非常方便和容易使用的,试一试。在一行中完成所有类型的请求,
发布于 2016-01-21 19:24:58
问题是我需要con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
https://stackoverflow.com/questions/34919630
复制相似问题