首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同手机的不同结果

不同手机的不同结果
EN

Stack Overflow用户
提问于 2016-07-03 18:18:14
回答 1查看 41关注 0票数 0

我有两部手机: lg g3,我从中国那里买的,lg g3,我从以色列买的。

我构建了一个android应用程序,它可以根据关键字搜索从网络获得响应(键搜索可以是任何语言:俄语、希伯来语、阿拉伯语、英语等)。

在英语中,这两款手机都很好用。

但当我使用非英语语言(以上,没有尝试中文)以色列手机仍然工作很好,但中国手机没有。

当我调试中国手机中的程序时,我发现当我得到回复时,键搜索(在非英语语言中)是在问号中。但在以色列的手机,它的工作很好,所以我尝试了各种编码,但似乎没有任何工作。

下面是代码中有问题的部分:

代码语言:javascript
复制
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(); 
            }
        }

问题是:我是否需要修改代码,这样中国的手机就可以接受其他语言(不仅仅是英语)?如果是这样的话,如果有人能告诉我答案,那就太好了。如果没有,那么也许我需要改变手机上的设置?这两款手机都有相同的操作系统语言(希伯来语)

谢谢你们所有人。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-04 03:59:15

所以utf-8是正确的,我不需要在电话上更改任何本地语言,只需对密钥搜索(URLEncoder.encode(关键字,“utf-8”)进行编码)。

这是完整的答案:

代码语言:javascript
复制
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(); 
            }
        }

谢谢大家的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38172987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档