0

I am making a app which listens to clipboard changes and uses some API to identify the language and get its meaning. The problem is that some languages are appearing as question marks. I think there is problem with it character encoding. I don't know how to fix it.

The code:

 //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;

}

The data returned says that the word was just question marks although it displays properly is logcat.

The log:

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":{}}

1 Answers1

0

As current_line is a String and hence it is unable to recognize the Hindi characters.

Because I see the response is correct.enter image description here

Use below to read the response

 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
Shishram
  • 1,514
  • 11
  • 20
Shono
  • 57
  • 8
  • I tried it but the problem is not with response because even earlier i was able to get meaning of English words in Hindi. – anjay goel Feb 19 '16 at 08:59