我正在创建一个应用程序来监听剪贴板更改并使用一些API来识别语言并获得其含义。问题是某些语言出现在问号上。我认为字符编码存在问题。我不知道如何解决它。
代码:
//to get language
static void getLanguage (String s) {
URL get = null;
String data = null;
try {
get = new URL("https://translate.yandex.net/api/v1.5/tr.json/detect?key=<api_key>&text=" + s);
Log.d(app_name,get.toString());
InputStreamReader i_read = new InputStreamReader(get.openConnection().getInputStream());
data = "";
int char_read;
while ((char_read = i_read.read()) != -1) {
data += (char) char_read;
}
int start_index = data.lastIndexOf(":") + 2;
int end_index = data.lastIndexOf("\"");
lang = data.substring(start_index, end_index);
if (lang.equals("")) lang = "en";
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Log.d(app_name, data);
}
}
//to get meaning
static public String getDef(String word_string,boolean reTranslate) {
Log.d (app_name,"got word to define: "+word_string);
if( !testInet("google.com"))
{
Log.d (app_name,"no internet Found");
}
String dict_data = "";
try {
if (auto_detect_language && !reTranslate)getLanguage(word_string);
URL get = new URL("https://glosbe.com/gapi/translate?from="+lang+"&dest="+lang_to_translate+"&format=json&phrase=" + word_string+"&tm=true");
Log.d(app_name,get.toString());
URLConnection connect = get.openConnection();
HttpURLConnection con = (HttpURLConnection) connect;
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String current_line;
while ((current_line = in.readLine()) != null) {
dict_data += current_line;
}
Log.d (app_name,dict_data);
con.disconnect();
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return dict_data;
}
返回的数据表明这个单词只是问号,虽然它正确显示是logcat。
日志:
02-19 09:28:48.293 31626-1764/com.anjay.dictionary D/Dictionary: got word to define: शब्दकोश
02-19 09:28:48.523 31626-1763/com.anjay.dictionary D/Dictionary:https://translate.yandex.net/api/v1.5/tr.json/detect?key=<api key>&text=शब्दकोश
02-19 09:28:50.363 31626-1763/com.anjay.dictionary D/Dictionary: {"code":200,"lang":""}
02-19 09:21:13.983 21912-25637/com.anjay.dictionary D/Dictionary: https://glosbe.com/gapi/translate?from=en&dest=en&format=json&phrase=शब्दकोश&tm=true
02-19 09:21:15.683 21912-25648/com.anjay.dictionary D/Dictionary: {"result":"ok","phrase":"??????","examples":[],"from":"en","dest":"en","authors":{}}