我尝试使用
从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'}
我怎样才能让它发挥作用?谢谢!
答案 0 :(得分:0)
正如评论所说(我的评论声誉太低),{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}无效上下文
我也建议你使用Koush Ion库来获取json请求。 https://github.com/koush/ion
非常方便易用,试一试。在一行中完成了所有类型的请求,
答案 1 :(得分:0)
问题在于我需要con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");