Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Android向Nodejs服务器发送JSON_Java_Android_Node.js - Fatal编程技术网

Java 从Android向Nodejs服务器发送JSON

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

我尝试使用以下命令从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();
当我打印从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。。。