使用Android + GAE Cloud Endpoints进行混乱的字符编码

时间:2013-06-18 11:15:01

标签: android google-app-engine character-encoding google-cloud-endpoints

我使用GAE的Cloud Endpoints制作了网络应用程序。应用程序只有后端部分。 应用程序调用Google Places API并解析JSON响应,创建返回客户端的对象。客户端是Android应用程序,它使用GAE生成的客户端库。

我的问题如下:在本地开发服务器上运行的应用程序在Android上正确显示UTF-8格式的字符串,但已部署的应用程序在Android上显示乱糟糟的字符串。 例如:而不是KliničkiCentar,它显示了Klini kiCentar。

我正在使用最新的Fedeora GNU / Linux,在Eclipse Kepler(最新版本)中开发,GAE是版本1.8.1,Google插件适用于Eclipse版本3.2.4(最新版本)。

我已经失去了不可思议的时间来解决这个问题。 我假设解决方案是一些强制UTF-8的配置行。 提一下,我已经在appengine-web.xml下面了:

<system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
    <property name="file.encoding" value="UTF-8" />
    <property name="DEFAULT_ENCODING" value="UTF-8" />
</system-properties>

提前感谢您的每一个建议。

1 个答案:

答案 0 :(得分:4)

解决了它!!!!

我认为这是Google的错误,就像我的一样。 我假设响应将以UTF-8编码,所以我只使用了:

URL url = new URL(PLACE_AC_API + encodedInput + PLACE_AC_API_OPTIONS);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

明确添加charset参数一切顺利:

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

但谷歌肯定需要了解为什么在本地和部署中这种行为有所不同。

对每个人都是最好的,感谢有趣和努力!