2016-07-03 90 views
0

我有2個手機: LG G3,我從中國 買和LG G3,我從以色列不同的結果

買我根據keysearch建立一個Android應用程序,會從網頁的響應(關鍵詞搜索可以使用任何語言:俄語,希伯來語,阿拉伯語,英語等)

在英語中,這兩款手機都很棒。

但是,當我使用非英語的語言(以上所有,沒有嘗試中文)以色列電話仍然很好,但中國電話沒有。

當我調試中國電話的程序時,我看到keysearch(非英語語言)時,當我得到響應是在問號。但在Isreal手機中效果很好,所以我嘗試了各種編碼,但似乎沒有任何工作。

這裏是有問題的部分代碼:

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(); 
      } 
     } 

的問題是:我是否需要在代碼中改變一些東西,因此中國手機會接受其他漢語語言(不只是英語)?如果是這樣,如果有人能指導我找到答案,那將是非常好的。如果沒有,那麼也許我需要更改手機上的設置?兩個手機的操作系統語言相同(希伯來語)

謝謝大家。

+0

中國不使用UTF-8,因爲它不使用普通字符集 – Zoe

+0

感謝您的回覆,所以如果不是utf-8,我應該使用什麼字符集來支持所有語言?也許我需要根據手機的工廠語言來調整編碼,並且我會得到正確的響應? – Joe

+0

https://en.wikipedia.org/wiki/Chinese_character_encoding – Zoe

回答

0

所以utf-8是正確的,我不需要在手機上更改任何本地語言,我只需要編碼keysearch(URLEncoder.encode(關鍵字,「UTF-8」))。

這是一個完整的答案:

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(); 
      } 
     } 

謝謝大家的幫助。