Java:预览HttpPost请求

EZF*_*rag 8 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);
Run Code Online (Sandbox Code Playgroud)

这是我的请求的基本设置.这是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}}
Run Code Online (Sandbox Code Playgroud)

如果这对你来说很合适,我会开始向管理员发送垃圾邮件,但是现在,我需要知道我是否遗漏了一些东西.

EZF*_*rag 9

我发现了这个问题......服务器对内容类型标题和内容格式非常敏感

    httpost.setHeader("Content-type", "application/json;charset=utf8");
Run Code Online (Sandbox Code Playgroud)

需要改为

    httpost.setHeader("Content-type", "application/json; charset=utf-8");
Run Code Online (Sandbox Code Playgroud)

和StringEntity se = new StringEntity(JSONRequest);

需要改为

     StringEntity se = new StringEntity(JSONRequest,"utf-8");
Run Code Online (Sandbox Code Playgroud)

谢谢Jens,一条评论将我推向了正确的方向.