Search code examples
androidhttpconnectionlg

different outcome in different phones


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.


Solution

  • 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.