简体   繁体   English

如何将json添加到java中的http帖子的主体

[英]How to add json to the body of an http post in java

I'm trying to post some JSON data in java for an Android app I'm working on. 我正在尝试在java中为我正在处理的Android应用发布一些JSON数据。 Is the below valid or do I need to push the JSON string in a different way? 以下是有效的还是我需要以不同的方式推送JSON字符串?

HttpPost httpost = new HttpPost("http://test.localhost");
httpost.setEntity(new StringEntity("{\"filters\":true}"));
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
//... other java code to execute the apache httpclient

Thank you in advance 先感谢您

You should set the Content-Type header to "application/json" . 您应该将Content-Type标头设置为"application/json" Everything else looks good. 其他一切看起来都不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM