I have the following method which retrieves a JSON string from a remote REST API. The JSON string contains French characters. The problem is that the French text in the JSON string is mangled.
I am using the GluonConnect REST client library to fetch the JSON string from the remote server. Everything is fine except for the retrieval of JSON with non-English text.
Here is my method:
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());
}
}
});
}
The key line is response.bind(godp.asString()) . godp.AsString() returns mangled text. For example, the word "Médiateur" with an accented é is displayed as "Médiateur". If I change the line to response.bind(godp.asString(Locale.FRANCE, null)) , then nothing is returned.
Please what am I doing wrong? Thanks a lot for your kind assistance.
Thanks a lot for your suggestions, I finally got it to work by bypassing the StringInputConverter class in the GluonConnect library. I wrote a new slightly modified class based on the StringInputConverter class as shown below:
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;
}
}
}
All I did was change BufferedReader(new InputStreamReader(getInputStream())) to BufferedReader(new InputStreamReader(getInputStream(), Charset.forName("UTF-8"))) .
I then used it my retrieveJsonString method as follows:
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());
}
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.