其他分享
首页 > 其他分享> > 特殊字符引起REST Web服务通信问题

特殊字符引起REST Web服务通信问题

作者:互联网

我正在尝试将JSON对象从Android应用程序发布到REST Web服务.一切正常,直到我添加å,ä,ö等特殊字符.

JSONObject absenceObject = new JSONObject();
absenceObject.put(INFO_DESCRIPTION, "åka pendeltåg");
StringEntity entity = new StringEntity(absenceObject.toString());
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json";character);
httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
HttpResponse response = httpclient.execute(httpPost);

如果我打印missingObject.toString()并将结果复制到常规的rest客户端中,它也可以正常工作.

解决方法:

尝试在StringEntity构造函数中指定所需的字符集:

StringEntity entity = new StringEntity(absenceObject.toString(), "UTF-8");

标签:android,json,rest,special-characters
来源: https://codeday.me/bug/20191013/1904737.html