简体   繁体   English

App Engine中断编码

[英]App Engine Breaks Encoding

When I deploy the backend locally and make the call to the apimethod (through url or explorer) I receive a response with this in the JSON: 当我在本地部署后端并调用api方法(通过url或资源管理器)时,我在JSON中收到与此相关的响应:

"pronunciations": [  {   
    "type": "ahd-legacy",   
    "pronunciation": "(rēˈstrə)"  
} ]

However, the second I deploy it to the app engine and call this method (which stores the object in objectify) and then sends it back in that object format I am receiving this as the JSON: 但是,第二次我将其部署到应用程序引擎并调用此方法(将对象存储在objectify中),然后以该对象格式发送回该对象,我将其作为JSON接收:

"pronunciations": [  {
    "type": "ahd-legacy",
    "pronunciation": "(r����str��)"
} ]

I have also tried storing the string as utf-8 bytes (which objectify automatically converts to base64 which then still converts to the above) 我也尝试过将字符串存储为utf-8字节(对象自动转换为base64,然后仍转换为上述字符串)

Should I be tagging something specifically so it is stored correctly? 我应该专门标记一些东西以便正确存储吗?

I figured out what it is. 我知道是什么。 The reason why this works fine when run from local and doesn't work when you deploy is because when I made a call to receieve something from an endpoint, it automatically uses the encoding for the server which is ASCII. 从本地运行时此方法工作正常,而在部署时则不工作的原因是,当我调用从端点接收消息时,它会自动为服务器使用ASCII编码。 If you want this to work you need to make your request like this: 如果您希望此方法有效,则需要这样发出请求:

URL url = new URL(uri);
String response = new BufferedReader(
    new InputStreamReader(url.openStream(), "UTF-8")).readLine();

JSONParser jsonParser = new JSONParser();
return jsonParser.parse(response);

This is probably not a storage problem, but more likely an encoding problem at the point where you receive/send data from/to the network and encode/decode it. 这可能不是存储问题,但更可能是在您从网络接收/发送数据并对其进行编码/解码时出现的编码问题。

JVM on GAE production servers is set to have US-ASCII as default encoding. GAE生产服务器上的JVM设置为使用US-ASCII作为默认编码。 On your local machine it's probably set to UTF-8. 在您的本地计算机上,它可能设置为UTF-8。

Whenever you convert between String and byte arrays, you should always explicitly use UTF-8. 每当在字符串数组和字节数组之间进行转换时,都应始终显式使用UTF-8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM