OkHttp不在UTF8中回复我,而是在百分比编码中回复

问题描述 投票:0回答:1

im用OkHttp(最新版本)调用GET Api(ModernMTT的API),我的问题是来自服务器的回复不是UTF-8,而是百分比编码,我不明白为什么,我尝试设置MINE在UTF-8中没有结果。服务器从CURL和Postman答复良好。所以问题出在OkHttp上。我的代码

public static String translate(String string) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(new FixEncodingInterceptor()).build();
        Request request = new Request.Builder()
                .url("https://api.modernmt.eu/translate?source=" + sourceLanguage + "&target=" + targetLanguage + "&q=" + string)
                .method("GET", null)
                .addHeader("MMT-ApiKey", apiKey)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            String stringTranslated= Objects.requireNonNull(response.body()).string();
            ResponseMTT responseMTT = new Gson().fromJson(stringTranslated, ResponseMTT.class);
            StandardCharsets.UTF_8));
            return responseMTT.getData().getTranslation();
        }
    }

response.body.string()返回此格式:

%22Sconto%20del%2020%:%20%3CPAColor0xFF66CC33%3EUtilities%20%E2%86%92%20Capacity%20of%20load%3CPAOldColor%3E.
java rest api okhttp machine-translation
1个回答
0
投票

您可以尝试将“ Accept-Charset”标头设置为UTF-8并测试您的代码吗?我假设邮递员默认将接受字符集标头设置为utf-8。

© www.soinside.com 2019 - 2024. All rights reserved.