如何使用HttpURLConnection发布JSON数据?我想这一点:cURL和HttpURLConnection - 发布JSON数据
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
StringReader reader = new StringReader("{'value': 7.5}");
OutputStream os = httpcon.getOutputStream();
char[] buffer = new char[4096];
int bytes_read;
while((bytes_read = reader.read(buffer)) != -1) {
os.write(buffer, 0, bytes_read);// I am getting compilation error here
}
os.close();
我得到的编译错误在第14行
卷曲请求:
curl -H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "{'value': 7.5}" \
"a URL"
这是处理卷曲请求的方式?任何信息对我都很有帮助。
谢谢。
您应该发布编译错误 – ykaganovich 2012-03-08 19:08:58