Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.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 使用JSON发布外来字符会产生400个字符_Java_Android_Json_Rest - Fatal编程技术网

Java 使用JSON发布外来字符会产生400个字符

Java 使用JSON发布外来字符会产生400个字符,java,android,json,rest,Java,Android,Json,Rest,我正在尝试使用带有外来字符的JSON发出POST请求,例如上面带有“~”的西班牙语n,但我一直收到此请求和响应错误: POST ... Accept: application/json Content-Type: application/json; charset=UTF-8 Content-Length: 151 Content-Encoding: UTF-8 Host: ... Connection: Keep-Alive User-Agent: .. {"numbers":"2","da

我正在尝试使用带有外来字符的JSON发出POST请求,例如上面带有“~”的西班牙语n,但我一直收到此请求和响应错误:

POST ...
Accept: application/json
Content-Type: application/json; charset=UTF-8
Content-Length: 151
Content-Encoding: UTF-8
Host: ...
Connection: Keep-Alive
User-Agent: ..

{"numbers":"2","date":"2014-07-15T00:00:00+0000","description":" // this never gets closed

X-Powered-By: ...
Set-Cookie: ...
Cache-Control: ...
Date: Tue, 15 Jul 2014 15:19:12 GMT
Content-Type: application/json
Allow: GET, POST

{"status":"error",
"status_code":400,
"status_text":"Bad Request",
"current_content":"",
"message":"Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)"}
我已经可以用普通的ASCII字符成功地发出POST请求,但现在我支持外语,我需要将外来字符转换为UTF-8(或任何正确的编码),除非有更好的方法

这是我的密码:

JSONObject jsonObject = new JSONObject();
HttpResponse resp = null;
String urlrest = // some url;
HttpPost p = new HttpPost(urlrest);
HttpClient hc = new DefaultHttpClient();
hc = sslClient(hc);

try
{
   p.setHeader("Accept", "application/json");
   p.setHeader("Content-Type", "application/json");

   // setting TimeZone stuff

   jsonObject.put("date", date);
   jsonObject.put("description", description);
   jsonObject.put("numbers", numbers);

   String seStr = jsonObject.toString();
   StringEntity se = new StringEntity(seStr);
   // Answer: The above line becomes new StringEntity(seStr, "UTF-8");

   Header encoding = se.getContentType();
   se.setContentEncoding("UTF-8");
   se.setContentType("application/json");
   p.setEntity(se);
   resp = hc.execute(p);
当我在se提交之前放置断点并查看它时,字符看起来是正确的


更新:用上面几行的答案更新代码,并用注释标识它。

新StringEntity构造函数采用“UTF-8”参数。

这是否可能:
jsonObject.toString(“UTF-8”)?否,唯一的参数
toString
接受的是一个
int
,用于许多缩进。您是否尝试在没有所有这些转换的情况下输入描述?是的,这就是导致此错误的原因。尝试为utf-8提供一个额外的参数
new StringEntity…