GluonConnect REST 库破坏 JSON 字符串中的非英文字符

GluonConnect REST library mangles non-English characters in a JSON string

我有以下方法从远程 REST API 检索 JSON 字符串。 JSON 字符串包含法语字符。问题是 JSON 字符串中的法语文本被破坏了。

我正在使用 GluonConnect REST 客户端库从远程服务器获取 JSON 字符串。除了用非英文文本检索 JSON 外,一切都很好。

这是我的方法:

public void retrieveJsonString() {
  // GluonConnect RestClient setup
  RestClient restClient = RestClient.create().host(this.host).path(this.path).queryParam("schema", this.schema).queryParam("uri", "/contactsform").method("GET");
  // GluonConnect GluonObservableObject setup
  GluonObservableObject<String> godp = DataProvider.retrieveObject(restClient.createObjectDataReader(String.class));
  // Add a listener to the GluonObservableObject
  godp.stateProperty().addListener(new InvalidationListener() {
    @Override
    public void invalidated(Observable arg0) {
      if (godp.getState().equals(ConnectState.SUCCEEDED)) {
        response.bind(godp.asString());
      }
    }
  });
}

关键行是response.bind(godp.asString())。 godp.AsString() returns 损坏的文本。例如,带重音符 é 的单词 "Médiateur" 显示为 "Médiateur"。如果我将行更改为 response.bind(godp.asString(Locale.FRANCE, null)),则不会返回任何内容。

请问我做错了什么?非常感谢您的帮助。

非常感谢您的建议,我终于通过绕过 GluonConnect 库中的 StringInputConverter class 使其工作。我在 StringInputConverter class 的基础上写了一个新的稍微修改过的 class 如下所示:

public class MyCustomStringInputConverter extends InputStreamInputConverter<String> {

private static final Logger LOGGER = Logger.getLogger(StringInputConverter.class.getName());

@Override
public String read() {
  try (StringWriter stringWriter = new StringWriter()) {
    try (
      BufferedReader reader =
          new BufferedReader(new InputStreamReader(getInputStream(), Charset.forName("UTF-8")));
      BufferedWriter writer = new BufferedWriter(stringWriter)) {
    boolean firstWrite = true;
    String line;
    while ((line = reader.readLine()) != null) {
      if (firstWrite) {
        firstWrite = false;
      } else {
        writer.newLine();
      }
      writer.write(line);
    }
  }

  return stringWriter.toString();
  } catch (IOException ex) {
    LOGGER.log(Level.SEVERE, "La lecture du flux d'entrée à échoué.", ex);
    return null;
  }
 }

}

我所做的只是将 BufferedReader(new InputStreamReader(getInputStream())) 更改为 BufferedReader(new InputStreamReader(getInputStream(), Charset.forName ("UTF-8")))

然后我使用我的 retrieveJsonString 方法如下:

  public void retrieveJsonString() {

  // RestClient setup
  RestClient restClient =
    RestClient.create().host(this.host).path(this.path).queryParam("schema", this.schema)
        .queryParam("uri", "/contactsform").method("GET");


  MyCustomStringInputConverter converter = new MyCustomStringInputConverter();

  // GluonObservableObject setup
  GluonObservableObject<String> godp =
    DataProvider.retrieveObject(restClient.createObjectDataReader(converter));

  // Add a listener to the GluonObservableObject
  godp.stateProperty().addListener(new InvalidationListener() {
    @Override
    public void invalidated(Observable arg0) {
      if (godp.getState().equals(ConnectState.SUCCEEDED)) {
        response.bind(godp.asString()); 
      } else if (godp.getState().equals(ConnectState.FAILED)) {
        exceptionMsg.set(godp.getException().getMessage());
      }
      }
  });

}