Java 从Android向Nodejs服务器发送JSON
我尝试使用以下命令从Android设备向Nodejs服务器发送请求Java 从Android向Nodejs服务器发送JSON,java,android,node.js,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
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();
当我打印从node js收到的请求时,我得到:
{
"{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}": ""
}
而不是
{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}
我怎样才能让它工作?谢谢 正如评论所说(我的声誉太低,无法发表评论),{'restaurant_id':'569a16e28dcdc5c8add2a8e0'是无效的上下文
我还建议您对json请求使用Koush Ion库
这是非常方便和易于使用的,给它一个尝试。在一行中,所有类型的请求都完成了,问题是我需要
con.setRequestProperty(“Content-Type”,“application/json;charset=UTF-8”)代码>{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}
不是有效的json。。。