【问题标题】:Strings with high Unicode characters become corrupted when passing from Javascript to Java从 Javascript 传递到 Java 时,具有高 Unicode 字符的字符串会损坏
【发布时间】:2019-12-09 02:29:45
【问题描述】:

我将一个字符串从 Javascript 传递给 React Native 本机 Java 模块,然后返回给 Javascript。但是,任何高 Unicode 字符(例如表情符号)在传递给 Java 后都会损坏并变成一对问号。

例如字符串"testing123????"变成"testing123??"

我怎样才能解决这个问题,让角色保持他们的价值观?

编辑: React Native 后台上传库正在处理该字符串。以下是该库中将文本(位于 parameters 字段中)传递给 Java 模块的代码摘录:

import { NativeModules, DeviceEventEmitter } from 'react-native'
export type StartUploadArgs = {
  url: string,
  path: string,
  method?: 'PUT' | 'POST',
  // Optional, because raw is default
  type?: 'raw' | 'multipart',
  // This option is needed for multipart type
  field?: string,
  customUploadId?: string,
  // parameters are supported only in multipart type
  parameters?: { [string]: string },
  headers?: Object,
  notification?: NotificationArgs
}
const NativeModule = NativeModules.VydiaRNFileUploader || NativeModules.RNFileUploader // iOS is VydiaRNFileUploader and Android is NativeModules
//...
export const startUpload = (options: StartUploadArgs): Promise<string> => NativeModule.startUpload(options)

这里是处理字符串的 Java 代码的摘录:

  @ReactMethod
  public void startUpload(ReadableMap options, final Promise promise) {
//...
      HttpUploadRequest<?> request;

      if (requestType.equals("raw")) {
        request = new BinaryUploadRequest(this.getReactApplicationContext(), customUploadId, url)
                .setFileToUpload(filePath);
      } else {
        if (!options.hasKey("field")) {
          promise.reject(new IllegalArgumentException("field is required field for multipart type."));
          return;
        }

        if (options.getType("field") != ReadableType.String) {
          promise.reject(new IllegalArgumentException("field must be string."));
          return;
        }

        request = new MultipartUploadRequest(this.getReactApplicationContext(), customUploadId, url)
                .addFileToUpload(filePath, options.getString("field"));
      }


      request.setMethod(method)
        .setMaxRetries(2)
        .setDelegate(statusDelegate);
//...
      if (options.hasKey("parameters")) {
        if (requestType.equals("raw")) {
          promise.reject(new IllegalArgumentException("Parameters supported only in multipart type"));
          return;
        }

        ReadableMap parameters = options.getMap("parameters");
        ReadableMapKeySetIterator keys = parameters.keySetIterator();

        while (keys.hasNextKey()) {
          String key = keys.nextKey();

          if (parameters.getType(key) != ReadableType.String) {
            promise.reject(new IllegalArgumentException("Parameters must be string key/values. Value was invalid for '" + key + "'"));
            return;
          }
          request.addParameter(key, parameters.getString(key));
        }
      }
//...
      String uploadId = request.startUpload();
      promise.resolve(uploadId);
  }

【问题讨论】:

  • 请分享您提取此字符串的 Java 代码。这就像那里的编码问题。
  • 在传递给 Java 或任何其他平台之前,您需要转义 unicode 字符。参考this方法。
  • 首先显示将字符串从 JavaScript 发送到 java 的代码 - 你可能在那里编码有问题。或者,但不太可能,您在接收方做错了什么。
  • 我已经用处理字符串的 JS 和 Java 代码更新了问题。

标签: javascript java react-native unicode


【解决方案1】:

java servlet 规范假定表单参数默认为 ISO-8859-1。假设您使用的是 tomcat,请参阅 https://cwiki.apache.org/confluence/display/TOMCAT/Character+Encoding 了解如何解决此问题

来自页面的相关引用

POST 请求应指定参数和值的编码 他们发送。由于许多客户端未能设置显式编码,因此 默认使用的是 US-ASCII 用于 application/x-www-form-urlencoded 和 ISO-8859-1 适用于所有其他内容类型。

相关SO帖子https://stackoverflow.com/a/19409520/1967484

请记住,您的控制台和数据库也可能不支持高 unicode 字符。

【讨论】:

  • 我没有在我的应用程序中使用 Java servlet。 Java 端将字符串发送到节点服务器,但似乎文本在发送之前已损坏。
  • 你的??问题是编码问题的明显迹象。确保您在该过程中发出的每个 http 请求都设置了字符集,它可能会解决您的问题
  • 这就是问题所在。我使用的后台上传库没有公开将字符集设置为 UTF-8 的方法,但是一旦我手动设置它就可以正常工作。感谢您的帮助。
【解决方案2】:

像这样修改后台上传库的代码解决了这个问题:

request = new MultipartUploadRequest(this.getReactApplicationContext(), customUploadId, url)
        .addFileToUpload(filePath, options.getString("field"))
        .setUtf8Charset(); // add this line

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多