使JSON具有服务器可接受的复杂字符串

时间:2013-10-31 04:55:48

标签: json http-error

我尝试将以下JSON上传到服务器:

{
    "sign": "cancer",
    "date": 30,
    "month": 10,
    "year": 2013,
    "reading": "vQdKU0SufpGmvkkyfvkdUr&yg/ rodatmifvkyfav ay:avjzpfrnf/ olwpfyg; rodapvkdaom udpörsm;udk rvkyfavaumif;avjzpfrnf/ vltrsm; olwpfyg;\ pdwf0ifpm;p&m jzpfaewufonf/ aiGaMu;udpö owdxm;NyD; udkifwG,fyg/ vuf0,faiGaysufaomaMumifh Mum;pdkufavsmf&udef; MuHKrnf/"
}

服务器返回HTTP 400。

我是否需要对JSON进行任何修改才能使服务器接受?

这是执行上传的代码:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");

        String input = "{\"sign\": \"" + reading.getSign() + "\"" + ", \"date\": " 
                + reading.getDate() + ", \"month\": " + reading.getMonth() 
                + ", \"year\": " + reading.getYear()
                + ", \"reading\": \"" + reading.getReading() + "\"}";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();                        

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

0 个答案:

没有答案