19

我尝试进行通话后并使用此值传递输入-“ä€爱لآहที่”我收到错误消息

{"error":{"code":"","message":{"lang":"en-US","value":{"type":"ODataInputError","message":"Bad Input: Invalid JSON format"}}}}

这是我的代码

    conn.setRequestMethod(ConnectionMethod.POST.toString());
    conn.setRequestProperty(CONTENT_LENGTH, Integer.toString(content.getBytes().length));
    conn.setRequestProperty("Accept-Charset", "UTF-8"); 
    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(content);
    wr.flush();
    wr.close();
    InputStream resultContentIS;
    String resultContent;
    try {
        resultContentIS = conn.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(resultContentIS));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }

它在 conn.getInputStream() 上失败;

内容的价值是

{ "input" : "ä€愛لآहที่" }

它在输入为字符串或整数的地方工作

当我添加声明时

   conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 

我收到了不同的信息

 {"error":{"code":"","message":{"lang":"en-US","value":{"type":"Error","message":"Internal server error"}}}}
4

3 回答 3

45

请尝试以下代码:

DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
writer.write(content);
writer.close();
wr.close();

您应该使用 JSONObject 来传递参数

输入,请尝试

BufferedReader reader = new BufferedReader(new InputStreamReader(resultContentIS, "UTF-8"));

如果输出是:?????????,所以不用担心,因为您的输出控制台不支持 UTF-8

于 2013-09-12T09:11:33.217 回答
3

似乎您的变量内容确实已经包含错误的数据,因为您可能在没有注意所需编码的情况下转换了字符串。

在编写器上设置正确的编码并使用write()而不是writeBytes()应该值得一试。

于 2013-09-12T08:41:39.007 回答
1

You have to send content via byte array

 DataOutputStream outputStream= new DataOutputStream(conn.getOutputStream());
 outputStream.write(content.toString().getBytes());

This is completely solution for your file name character problems. The imported point is string sending via byte array. Every character changing via byte character. This is prevent your character encoding problems.

于 2017-11-25T16:47:08.927 回答