问题描述
情况
我正在使用作为我的Android应用程序的后端服务器。在Cron-Job中,服务器定期下载新数据(String),过滤相关信息,并通过Firebase Cloud Messaging将其发送到不同的主题(通过将HTTP请求发布到)。
I'm using AppEngine Cloud Endpoints (Java) as backendserver for my Android-application. In a Cron-Job the server regularly downloads new data (String), filters out relevant information and sends it via Firebase Cloud Messaging to different topics (by posting HTTP-Request to https://fcm.googleapis.com/fcm/send).
(很简单)问题
下载的文本是德文,包含ä,ö,ü,ß等字母(没有着名的这些字符的istead) 我想将编码更改为UTF-8 。
The downloaded text is in German, containing letters like ä,ö,ü,ß and (to not have the famous ?s istead of these characters) I want to change the encoding to UTF-8.
部署到AppEngine时,US-ASCII编码是默认值(默认情况下为什么不是UTF-8 ...)。在本地测试时,使用我的电脑的默认编码是UTF-8,而 一切正常工作 。
US-ASCII encoding is the default when deployed to AppEngine (why isn't it UTF-8 by default...). When tested locally, the default encoding of my computer is used which is UTF-8 and everything is working just fine.
所以我想要的是 Charset.getDefaultCharset()
返回 UTF-8
。
我该如何实现?
提前感谢所有的帮助赞赏。
So all I want is Charset.getDefaultCharset()
to return UTF-8
.
How do I achieve this?Thanks in advance, all help appreciated.
我尝试了
-
将其添加到appengine-web.xml中:
adding this to appengine-web.xml:
< env-variables>
< env-var name =DEFAULT_ENCODINGvalue =UTF-8/>
< / env-variables>
在本地进行测试时,我遇到了不正确的环境变量异常。当部署编码仍然是ASCII时。
When testing locally I got IncorrectEnvironmentVariableException. When deployed encoding was still ASCII.
将其添加到< system-properties>
appengine-web.xml:
adding this to <system-properties>
-tag in appengine-web.xml:
< property name =file.encodingvalue =UTF-8/>
< property name =DEFAULT_ENCODINGvalue =UTF-8/>
部署时,但是当我尝试使用这种方式将编码设置为本地开发服务器上的US-ASCII时,UTF-8仍然由 Charset.getDefaultCharset()
changed nothing, not only when deployed but also when I tried to use this to set the encoding to US-ASCII on local developement server, still UTF-8 was returned by Charset.getDefaultCharset()
甚至尝试使用反射:
System.setProperty(file.encoding , UTF-8);
字段charset = Charset.class.getDeclaredField(defaultCharset);
charset.setAccessible(true);
charset.set(null,null);
(导致IllegalAccessException)
(caused IllegalAccessException)
尝试将HTTP编号设置为内容类型属性(不起作用):
tried setting encoding as content-type-property with the HTTP-post (didn't work):
连接.setRequestProperty(Content-Type,application / json; charset = UTF-8);
推荐答案
简短答案
如果您有编码问题(即'?'),而且 像更改default-charset一样解决了问题,那么你真正要做的就是要找到使用默认字符集进行编码的代码,并将自定义字符集传递给
Short Answer
If you have problems with encoding (i.e. '?'s) and it seems like changing default-charset solves the problem then what you really have to do is to find the code where the default-charset is used for encoding and pass a custom charset to be used instead.
(像@AndreiVolgin一样注释:你不应该依赖系统默认字符集。)
(Like @AndreiVolgin commented: You should never rely on the system default-charset.)
一个字符集指定如何将字符表示为数字(即字节)。
Java中所有字符串的内部字符串实际上都是UTF-16,所以自定义字符集变得重要的唯一时间是将字符串转换为字节数组( String.getBytes( )
)或反之亦然( new String(byte []
)。
这是默认如果没有传递如 String.getBytes(Charset)
或的新字符串(byte [],Charset))的字符集,则使用-charset code>。
A charset specifies how chars are represented as numbers (i.e. bytes).The internal charset of all Strings in Java is in fact UTF-16 so the only time custom charsets become important is when converting a String to a byte-Array (
String.getBytes()
) or vice versa (new String(byte[]
).
This is where the default-charset is used if you don't pass a charset like String.getBytes(Charset)
or new String(byte[], Charset)
.
当使用不同的字符集进行编码时,您会看到'?'(
String.getBytes() code>)和解码(
new String(byte []
)。
You get to see '?'s when different charsets are used for encoding (
String.getBytes()
) and decoding (new String(byte[]
).
所以要摆脱'?'将
String.getBytes()
替换为 String.getBytes(Charset)
(或可能替换 new String(byte [])
by new String(byte [],Charset)
So to get rid of the '?'s replace
String.getBytes()
by String.getBytes(Charset)
(or maybe replace new String(byte[])
by new String(byte[], Charset)
but don't know if thats a thing).
对于我来说,通过互联网发送一些文字时出现编码问题,因为串需要转换为
byte []
以使用 HttpURLConnection.getOutputStream()发送它。write(byte [])
。
For me the encoding problem occured when sending some text via the internet because the String needs to be converted to
byte[]
to send it using HttpURLConnection.getOutputStream().write(byte[])
.
确切地说:在课程
com.google.android.gcm.server.Sender
中,方法 post(java.lang.String,java.lang.String,java.lang.String)
,第471行是 byte [] bytes = body.getBytes( );
。
To be precise: In class
com.google.android.gcm.server.Sender
, method post(java.lang.String, java.lang.String, java.lang.String)
, line 471 was byte[] bytes = body.getBytes();
.
我改变了(覆盖了它)到
byte [] bytes = body.getBytes(UTF -8);
- 为我解决了。
I changed it (overwrote it) to
byte[] bytes = body.getBytes("UTF-8");
- solved it for me.
这篇关于Google AppEngine - 如何为Google AppEngine设置默认的Charset /文件编码(UTF-8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!