0

I have 2 phones: lg g3 that i bought from China and lg g3 that i bought from Israel

i build an android app that gets a response from web according to a keysearch(key search can be in any language:russian,hebrew, arabic, english etc.)

In english, both phones work great.

But when i use non-english langauge(all above, didn't try chinese) the Israel phone still works great but the China phone not.

When i debug the program in the China phone i saw that the keysearch (in non english langaage) when i get the reponse is in question marks. but in the Isreal phone it's works great, so i tried all kinds of encoding, but nothing seems to work.

here's the piece of the code that have the problem:

HttpURLConnection connection = null;
        try {
            //Create connection
            URL url = new URL("https://www.example.com/results?search_query="+keyword);
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(),"UTF-8"));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
                response.append('\r');
            }
            m_htmlDoc = response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            m_htmlDoc =  null;
        } finally {
            if(connection != null) {
                connection.disconnect(); 
            }
        }

the question is: do i need to change something in the code so the China phone will accept other langauges (not just english)? if so, it would be great if someone can direct me to the answer. if not, so maybe i need to change settings on the phone? both phones has the same OS langauge (hebrew)

Thank you all.

Joe
  • 171
  • 2
  • 19
  • China does not use UTF-8 because it doesn't use the common charset – Zoe Jul 03 '16 at 18:21
  • thank for the reply, so if not utf-8, what charset should i use for supporting all languages? maybe i need to to adjust the encoding according to the factory language of the phone and with that i'll get the response correct? – Joe Jul 03 '16 at 18:24
  • https://en.wikipedia.org/wiki/Chinese_character_encoding – Zoe Jul 03 '16 at 18:26
  • THat's absolutely not the problem. YOu're sending data to a server. Using utf-8 will work fine. If you're seeing results as ?? on screen, it means you don't have the proper font installed for that language, and possibly not the locale data as well. Try installing the locale to the phone – Gabe Sechan Jul 03 '16 at 18:30
  • Hi Gabe, if you mean intalling the locale to the phone like a keyboard input, i've already tried that but didn't worked. if you mean to change display language of the phone, i don't want my users to change their display language every time when they want to use this app – Joe Jul 03 '16 at 18:33

1 Answers1

0

so the utf-8 was correct and i didn't need tochange any local langauge on the phone all i needed is to encode the keysearch (URLEncoder.encode(keyword, "UTF-8")).

this is the complete answer:

HttpURLConnection connection = null;
        try {
            //Create connection
            URL url = new URL("https://www.example.com/results?search_query=" + URLEncoder.encode(keyword, "UTF-8"));
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(),"UTF-8"));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
                response.append('\r');
            }
            m_htmlDoc = response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            m_htmlDoc =  null;
        } finally {
            if(connection != null) {
                connection.disconnect(); 
            }
        }

Thank you all for the help.

Joe
  • 171
  • 2
  • 19