0
我正在製作一個應用程序,用於偵聽剪貼板更改並使用一些API來識別語言並獲取其含義。問題是有些語言顯示爲問號。我認爲它有字符編碼的問題。我不知道如何解決它。從剪貼板獲得的數據顯示爲問號。 Android
代碼:
//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":{}}
我嘗試過,但問題是不響應,因爲還早,我是能夠得到在印地文英語單詞的意思。 –