Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.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:预览HttpPost请求_Java_Android_Json - Fatal编程技术网

Java:预览HttpPost请求

Java:预览HttpPost请求,java,android,json,Java,Android,Json,我正在尝试使用Java将JSON格式的数据发送到服务器。信息正在传送到服务器,但服务器正在响应一个“错误的请求” 这是我请求的基本设置。以下是JSONData: {"clientApplicationDto":{"AuthenticationToken":"","BrandId":12,"MobileDeviceApplicationId":0},"mobileDeviceInfo":{"CarrierName":"MTN-SA","OsVersion":"2.2.2","ClientA

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

这是我请求的基本设置。以下是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}}

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

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

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

    httpost.setHeader("Content-type", "application/json; charset=utf-8");
及 StringEntity se=新的StringEntity(JSONRequest)

需要改成

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

谢谢Jens,这条评论把我推向了正确的方向。

试试这条,它对你有好处

    private static String sendRequestPost(String url, Object obj) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpPost httpost = new HttpPost(url);
            if (obj != null) {
                httpost.setEntity(new StringEntity(new Gson().toJson(obj), "utf-8"));
                System.out.println("Request Json => " + new Gson().toJson(obj));
            }
            httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json; charset=utf8");

            HttpResponse response = httpClient.execute(httpost);

            HttpEntity responseEntity = response.getEntity();
            String strResponse = EntityUtils.toString(responseEntity);
            return strResponse;
        }
        catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }

    }

陛下对于初学者来说,
StringEntity
(或者更确切地说,任何
AbstractHttpEntity
子类)都有一个内容类型,默认情况下,它将被设置为常量
HTTP.PLAIN\u type
,使用
HTTP.default\u content\u CHARSET
作为字符集。考虑通过调用<代码> SE > SET CordnType(“应用程序/JSON;字符集= UTF-8”)< /COD>来设置内容类型。顺便说一句,您的JSON看起来格式良好。为了检查post或java代码是否有问题,您是否在Chrome REST控制台中尝试过该请求?很高兴知道这对你有帮助^_^
    private static String sendRequestPost(String url, Object obj) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpPost httpost = new HttpPost(url);
            if (obj != null) {
                httpost.setEntity(new StringEntity(new Gson().toJson(obj), "utf-8"));
                System.out.println("Request Json => " + new Gson().toJson(obj));
            }
            httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json; charset=utf8");

            HttpResponse response = httpClient.execute(httpost);

            HttpEntity responseEntity = response.getEntity();
            String strResponse = EntityUtils.toString(responseEntity);
            return strResponse;
        }
        catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }

    }