Java:预览 HttpPost 请求

标签 java android json

我正在尝试使用 Java 将 JSON 格式的数据发送到服务器。信息正在到达服务器,但服务器正在响应“错误请求”。

    HttpPost httpost = new HttpPost(path);

    StringEntity se = new StringEntity(JSONRequest);

    //sets the post request as the resulting string
    httpost.setEntity(se);

    //sets a request header so the page receving the request will know what to do with it
    httpost.setHeader("Accept", "application/json");
    httpost.setHeader("Content-type", "application/json;charset=utf8");
    HttpResponse response = httpclient.execute(httpost);

这是我请求的基本设置。这是 JSONData:

    {"clientApplicationDto":{"AuthenticationToken":"","BrandId":12,"MobileDeviceApplicationId":0},"mobileDeviceInfo":{"CarrierName":"MTN-SA","OsVersion":"2.2.2","ClientApplicationVersion":"TEST","DeviceManufacturer":"HTC","DeviceName":"HTC Desire","DeviceUniqueId":"1e9766fa2ef4c53a","OsName":"8","ClientApplicationTypeId":3}}

如果你们觉得这很合适,我将开始向管理员发送垃圾邮件,但现在,我需要知道我是否遗漏了什么。

最佳答案

我发现了问题...服务器对内容类型 header 和内容格式极其敏感

    httpost.setHeader("Content-type", "application/json;charset=utf8");

需要改成

    httpost.setHeader("Content-type", "application/json; charset=utf-8");

和 StringEntity se = new StringEntity(JSONRequest);

需要改成

     StringEntity se = new StringEntity(JSONRequest,"utf-8");

谢谢 Jens,那条评论将我推向了正确的方向。

关于Java:预览 HttpPost 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8972627/

相关文章:

java - 如果我在 main 方法中执行 Thread.sleep(10000) ,子线程会发生什么

java - 以编程方式调整 vectordrawable 图标的大小

android - AndroidTest 拒绝访问外部存储的权限

android - 有没有办法禁用 Android ListView 动画?

java - 收银机返回剩余金额加上二次付款的零钱

java - Hibernate/DBunit 奇怪的行为

具有多个客户端的 Java 即时通讯工具无法正常工作

javascript - 带有空值的 ExtJS JSON 记录

java - 更新 JSONObject 中的元素

python - ElasticSearch:TypeError:预期的字符串还是缓冲区?