内容中的字符由服务器接收如下。当我在Mac OSX上运行相同的代码然后它按预期运行,但是当在Windows机器上运行相同的程序时,我得到这种行为,服务器代码没有问题,因为当我从Postman调用此API并通过这个字符然后由服务器正确接收。看来GsonBuilder在处理非英文字符方面存在一些问题。我也很少用其他语言来解决这个问题。
Εισιτή?ια
private String content = "Εισιτήρια";
public String post(String targetEnvironment) throws IOException{
String gson = new GsonBuilder().disableHtmlEscaping().excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT).setLenient().create().toJson(this).toString();
System.out.println(gson);
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, gson);
Request request = new Request.Builder()
.url(targetEnvironment)
.post(body)
.addHeader("content-type", "application/json; charset=utf-8")
.addHeader("accept", "application/json")
.addHeader("client_application_id", "1")
.addHeader("x-requested-with", "1")
.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)")
.addHeader("accept-encoding", "gzip,deflate,br")
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
String response_string = response.body().string();
System.out.println(response_string);
}