Java 在app engine数据存储中设置文本数据类型的编码
在我的带有app engine后端的android项目中,我使用文本数据类型来存储我的一个实体属性。与字符串数据类型不同,文本数据类型不支持Unicode,因此在从数据存储中检索该属性的数据时,我的编码会丢失。因此,我只能看到ascii字符,而所有其他字符都没有正确显示。如何为文本数据类型设置Unicode编码?感谢您的帮助 请参考下面的代码,我正在填充我的文本数据类型(quizcontent)并将其存储在app engine数据存储中。我从本地存储的文件中获取内容,填充到字符串中,然后将该字符串加载到appengine数据存储中的quizcontent属性(数据类型为Text)中。当我从app engine控制台在数据存储中查看此属性时,我可以看到Unicode编码正在丢失Java 在app engine数据存储中设置文本数据类型的编码,java,google-app-engine,google-cloud-platform,google-cloud-datastore,google-cloud-endpoints,Java,Google App Engine,Google Cloud Platform,Google Cloud Datastore,Google Cloud Endpoints,在我的带有app engine后端的android项目中,我使用文本数据类型来存储我的一个实体属性。与字符串数据类型不同,文本数据类型不支持Unicode,因此在从数据存储中检索该属性的数据时,我的编码会丢失。因此,我只能看到ascii字符,而所有其他字符都没有正确显示。如何为文本数据类型设置Unicode编码?感谢您的帮助 请参考下面的代码,我正在填充我的文本数据类型(quizcontent)并将其存储在app engine数据存储中。我从本地存储的文件中获取内容,填充到字符串中,然后将该字符
public class EndpointsInsertUpdateQuizContentTask extends AsyncTask<Context, Integer, Long>{
protected Long doInBackground(Context... contexts){
Quizcontenttableendpoint.Builder endpointBuilder = new Quizcontenttableendpoint.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(), new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { } });
Quizcontenttableendpoint endpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build();
try{
//get local file content into a string
int ch;
StringBuffer fileContent = new StringBuffer("");
FileInputStream fis;
//String quizContentString;
fis = getBaseContext().openFileInput(selectedQuiz);
while( (ch = fis.read()) != -1)
fileContent.append((char)ch);
String quizContentString = new String(fileContent);
QuizContentTable quizContentTable = new QuizContentTable();
quizContentTable.setQuizKey(quizKey);
quizContentTable.setQuizContent(quizContentString);
quizContentResult = endpoint.insertQuizContentTable(quizContentTable).execute();
}
catch(Exception e){
errMsg=e.toString();}
return (long) 0;
}
private ProgressDialog pdia;
@Override
protected void onPreExecute(){
super.onPreExecute();
pdia = new ProgressDialog(ctx);
pdia.setMessage("Loading");
pdia.show();
}
protected void onPostExecute(Long result1) {
pdia.dismiss();
公共类EndpointsUserTupDateQuizContentTask扩展了AsyncTask{
受保护的长doInBackground(上下文…上下文){
Quizcontenttableendpoint.Builder endpointBuilder=新的Quizcontenttableendpoint.Builder(
AndroidHttp.newCompatibleTransport(),new JacksonFactory(),new HttpRequestInitializer(){
公共无效初始化(HttpRequestHttpRequest){};
Quizcontenttableendpoint=CloudEndpointUtils.updateBuilder(endpointBuilder.build();
试一试{
//将本地文件内容转换为字符串
int-ch;
StringBufferFileContent=新的StringBuffer(“”);
文件输入流fis;
//字符串quizContentString;
fis=getBaseContext().openFileInput(SelectedQuike);
而((ch=fis.read())!=-1)
追加((char)ch);
String quizContentString=新字符串(fileContent);
QuizContentTable QuizContentTable=新的QuizContentTable();
setQuizKey(quizKey);
setQuizContent(quizContentString);
quizContentResult=endpoint.insertQuizContentTable(quizContentTable.execute();
}
捕获(例外e){
errMsg=e.toString();}
返回(长)0;
}
私家侦探;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pdia=新进度对话框(ctx);
pdia.setMessage(“加载”);
pdia.show();
}
PostExecute上受保护的void(长结果1){
pdia.discouse();
解决了问题。问题是我在将文件内容读入字符串时遗漏了UTF编码。将上面代码中的文件读取部分替换为下面的代码
可以使用文本参数设置编码属性[1] [1]
String str;
StringBuffer fileContent = new StringBuffer("");
BufferedReader in = new BufferedReader(new InputStreamReader(getBaseContext().openFileInput(selectedQuiz), "UTF-8"));
while ((str = in.readLine()) != null)
fileContent.append(str);
String quizContentString = new String(fileContent);
in.close();