gpt4 book ai didi

java - 从 Android 中的 Google 翻译读取任何语言字符串

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:53 25 4
gpt4 key购买 nike

我正在创建一个翻译应用程序,我从 Android 支持的语音识别器获取输入文本。示例:印地语、中文等。现在我想构建这样的查询 -

public JSONObject getTranslatedText() {
StringBuilder sb = new StringBuilder();
String http = "https://translation.googleapis.com/language/translate/v2?key=xyz";
JSONObject response = null;
String json = "";

HttpURLConnection urlConnection = null;
try {
URL url = new URL(http);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");

urlConnection.connect();

String line1 = "{\n" + " 'q': '" + inputString + "',\n" + " 'target': '" + targetcodeString + "'\n" + "}";

DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.writeBytes(line1);
out.flush();
out.close();

BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
json = sb.toString();
response = new JSONObject(json);
} catch (MalformedURLException e) {

} catch (IOException e) {

} catch (JSONException e) {

} finally {
if (urlConnection != null) urlConnection.disconnect();

}
return response;
}

问题是它没有正确编码,我得到这样的输出 -示例:对于印地语中的单词“How are you”,即“क्या हाल”,表示为 9 & HG 08 * E

请给我一些帮助。提前致谢。

最佳答案

尝试使用Html.fromHtml(yourTranslatedStr).toString()。我用印地语尝试过,效果很好。

关于java - 从 Android 中的 Google 翻译读取任何语言字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45370386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com