【问题标题】:Android app encoding problem - not display ÅÄÖ properlyAndroid 应用程序编码问题 - 无法正确显示 ÅÄÖ
【发布时间】:2011-07-19 15:50:42
【问题描述】:

当用户在 EditTextbox 中输入值“Nedskräpning”时,它会在数据库中保存为“Nedskräpning”。所以我假设这个错误是从应用程序的 Android 部分生成的。更新:

 <EditText android:id="@+id/commentTextBox"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent" 
                android:layout_weight="1"/>

java代码:

commentTextBox = (EditText) findViewById(R.id.commentTextBox);
crapport.setComment(commentTextBox.getText().toString());

然后这个crapport被保存在数据库中。

关于如何解决这个问题的任何提示?

更新:这是我的 Apache CXF 网络服务:

  @Path("/crapportService/")
public class CrapportServiceImpl implements CrapportService {

    @Autowired
    private CrapportController crapportController;

    @Override
    @Path("image")
    @POST
    @Produces(MediaType.TEXT_HTML)
    public String addReport(MultipartBody multipartBody) {

        List<Attachment> attachmentList = new ArrayList<Attachment>();
        attachmentList = multipartBody.getAllAttachments();

        if (attachmentList.size() == 0){
            return "No attachments";
        }

        try {
            crapportController.processReportFromClient(attachmentList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "Error: FileNotFoundException";
        } catch (NullPointerException e) {
            return "Error: NullPointerException";
        } catch (IOException e) {
            return "Error: IOException";
        }

        return "Success";
    }

}

【问题讨论】:

    标签: android mysql utf-8 character-encoding


    【解决方案1】:

    在我看来,您的数据库使用 ISO-8859-15 或 ISO-8859-1 作为编码,来自 Android 的字符串采用 UTF-8 格式,如果将高位字和 UTF-8 中的低位字存储到数据库中8 个“Ä”被分成两个 ISO-8859-1 对应项,使其成为“ä”。

    【讨论】:

    • 我检查了数据库,它可以处理 UTF-8 字符,它被配置为 Varchar。它以存储方式存储,因为我的网络服务以这种方式存储。 (我通过在数据库中手动插入 åäö 字符来理解这一点,这很有效)。所以可能问题出在网络服务上,请查看我的原始帖子以查看它的外观。
    • 那么,如果您将数据存储到数据库中,硬编码到您的数据库中,那么存储是否正确?如果没有,您的 jdbc 驱动程序可能存在配置问题,假设您想要存储一些 ISO 内容。或者,如果它被正确存储,您的 servlet 容器/应用程序服务器可能会假设 Web 服务帖子的编码错误(跟踪 Web 请求将对此有所帮助)。不幸的是,有很多东西会破坏你的编码。
    【解决方案2】:

    通过将字符编码参数传递给字符串创建来解决它。 例如)后端应用程序的控制器部分中的 new String("values", Charset.forName("UTF-8"))。

    【讨论】:

    • 小心:这或多或少只是一种解决方法,当您强制将字符串读取为 UTF-8 时,仍然有一些东西会弄乱您的数据并在它到达之前将其转换为某种 ISO 编码这个电话。
    • 我明白了,那得去看看。
    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 2021-09-17
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多