2016-01-21 51 views
0

我尝试使用发送JSON从安卓到的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(); 

从Android设备发送请求到的NodeJS服务器当我打印从节点JS接收到的请求,我得到:

{   
    "{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}": "" 
} 

而不是{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}

我该如何得到它的工作?谢谢!

+0

'{“restaurant_id”:“569a16e28dcdc5c8add2a8e0”}'是不是有效的JSON ... – Selvin

回答

0

的评论说,(我有信誉太低,评论),{“restaurant_id”:“569a16e28dcdc5c8add2a8e0”}是无效的情况下

我也建议你使用Koush离子库JSON请求。 https://github.com/koush/ion

这是非常方便和易于使用,试试看。在一个线上的所有类型的请求都做了,

0

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