我有一个数据库,它的所有列都设置为“hebrew_general_ci”。
当我尝试手动或通过 Postman 将希伯来语值插入我的数据库时,我可以看到数据库中的值确实是希伯来语。
但是,当我尝试从我的应用程序(android 应用程序 - 用 java 编码)插入值时,这些值变成问号 - ????
我尝试在应用程序本身将我的文本编码为 UTF-8,但没有成功。
这是应该执行此操作的代码:
private String POST(String url, String jsonParamsAsString) {
String result = "";
String fixedUrl = url.replace(" ","%20");
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(fixedUrl);
byte ptext[] = jsonParamsAsString.getBytes();
jsonParamsAsString = new String(ptext, "UTF-8");
StringEntity input = new StringEntity(jsonParamsAsString);
input.setContentType("application/json; charset=utf-8" );
//input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
result = convertInputStreamToString(response.getEntity().getContent());
/*byte ptext[] = result.getBytes();
result = new String(ptext, "UTF-8");*/
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
最佳答案
如果您正在运行最新版本的 MySQL 并且需要处理表情符号,则需要将数据库编码设置为 utf8/utf8_general_ci 或 utf8mb4/utf8mb4_general_ci。 Here是文档。基本上,您不需要将表设置为特定语言的特定字符集。我已经使用了上述设置,它可以开箱即用地处理阿拉伯语、俄语、中文、英语等,它与语言无关并且可以正常工作。祝你好运。
编辑:您还需要确保您的查询连接具有以下两个参数:useUnicode=yes
和 characterEncoding=UTF-8
关于java - MySQL - 希伯来字符成为数据库中的问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30488292/