简体   繁体   English

Google App Engine数据存储编码?

[英]Google App Engine datastore encoding?

I'm using the GAE datastore for a Java application, and storing some text that will be in numerous languages. 我正在将GAE数据存储区用于Java应用程序,并存储一些将使用多种语言的文本。 In my servlet, I'm first checking to see if there's any data in the data store, and, if not, I'm creating some, similar to the following: 在我的servlet中,我首先检查数据存储中是否有任何数据,如果没有,我正在创建一些数据,类似于以下内容:

ArrayList<Lang> list = new ArrayList<Lang>();
list.add(new Lang("EN", "English", 1));
list.add(new Lang("ES", "Español", 0));
//more languages here...

PersistenceManager pm = PMF.get().getPersistenceManager();
for(Lang l : list) {
  pm.makePersistent(l);
}

Since this is using JDO, I guess I should include the relevent parts of the Lang class too: 由于这是使用JDO,我想我也应该包括Lang类的相关部分:

@PersistenceCapable
public class Lang {
 @PrimaryKey
 private String code;
 @Persistent
 private String name;
 @Persistent
 private int popularity;
// getters & setters & constructors...
}

However, the non-ASCII characters are giving me grief. 但是,非ASCII字符让我感到悲伤。 I've set my Eclipse project to use the UTF-8 encoding instead of the default Cp1252, so I think I'm okay from that perspective, but when I use the App Engine Data Viewer to look at my data, that Español entry becomes Español, and when I click on it to view it, I get a 500 Server Error. 我已经将我的Eclipse项目设置为使用UTF-8编码而不是默认的Cp1252,所以我认为从这个角度来看我没问题,但是当我使用App Engine数据查看器查看我的数据时,该Español条目变为Español,当我点击它查看它时,我得到500服务器错误。 (There are some other entries with right-to-left text that don't even show up in the Data Viewer at all, but one problem at a time...) (还有一些其他条目具有从右到左的文本,甚至根本没有出现在数据查看器中,但一次只出现一个问题......)

Is there anything special I can do in my code to set the character encoding, or specify to GAE that the data I'm storing is UTF-8? 我可以在代码中设置字符编码,或指定GAE我存储的数据是UTF-8吗? Or is the problem on the Eclipse side, and is there something I should be doing with my Java code? 或者是Eclipse方面的问题,我的Java代码应该做些什么?

Fixed same issue by setting both request and response encoding to utf-8. 通过将请求和响应编码设置为utf-8来修复相同的问题。 Request encoding results in valid string stored in datastore, without it values will be stored as "????..." 请求编码会导致存储在数据存储区中的有效字符串,而不会将其存储为“???? ...”

Requests: if you use Apache HTTP Client, this is done in the following way: 请求:如果您使用Apache HTTP Client,则按以下方式完成:

Get request: 获取请求:

NameValuePair... params;
...
String url = urlBase + URLEncodedUtils.format(Arrays.asList(params), "UTF-8");
HttpGet httpGet = new HttpGet(url);

Post request: 发布请求:

NameValuePair... params;
...
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(params), "UTF-8"));

Response: if you build your response in HttpServlet, this is done in a following way: 响应:如果您在HttpServlet中构建响应,则可通过以下方式完成:

HttpServletResponse resp;
...
resp.setContentType("text/html; charset=utf-8");

Are you sure you have a problem with your data? 您确定自己的数据有问题吗? I also encountered the similar issues before but it turns out it's a problem in the Python version of the Data Viewer. 我之前也遇到过类似的问题,但事实证明它是数据查看器的Python版本中的一个问题。 I can retrieve my data fine in Java. 我可以用Java检索我的数据。

I had I think the same problem with encoding several month ago. 几个月前,我认为编码存在同样的问题。 You can take a look to my sources, maybe it'll help: 1) http://code.google.com/p/vocrecaptor/source/browse/trunk/vocrecaptorweb/src/com/vocrecaptor/web/server/DictionaryServiceImpl.java 您可以查看我的来源,也许它会有所帮助:1) http://code.google.com/p/vocrecaptor/source/browse/trunk/vocrecaptorweb/src/com/vocrecaptor/web/server/DictionaryServiceImpl的.java

2) And class /com/vocrecaptor/web/server/servlet/AbstractServiceServlet.java 2)和类/com/vocrecaptor/web/server/servlet/AbstractServiceServlet.java

i notice that you already set your Eclipse project to use UTF-8 text encoding. 我注意到您已经将Eclipse项目设置为使用UTF-8文本编码。 Did you double checked the text enconding of the Java file containing the string like "Español" ? 您是否仔细检查了包含像“Español”这样的字符串的Java文件的文本内容?

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

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