将JSON从Android发送到Nodejs服务器

时间:2016-01-21 09:21:10

标签: java android node.js

我尝试使用

从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'}

我怎样才能让它发挥作用?谢谢!

2 个答案:

答案 0 :(得分:0)

正如评论所说(我的评论声誉太低),{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}无效上下文

我也建议你使用Koush Ion库来获取json请求。 https://github.com/koush/ion

非常方便易用,试一试。在一行中完成了所有类型的请求,

答案 1 :(得分:0)

问题在于我需要con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

相关问题