【问题标题】:diamonds with question marks from google results来自谷歌搜索结果的带问号的钻石
【发布时间】:2012-09-14 19:30:55
【问题描述】:

我遇到了下一个编码问题:查询谷歌时
起初用户代理是硬编码的:像这样

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0"

我在设备“A”上收到 有效结果,但在设备“B”上,方法: httpConnection.getResponseCode();
返回time out exception.
然后我决定尝试一些巫术,我将“用户代理”更改为:

String userAgent = System.getProperty("http.agent");

现在我在设备“B”(希伯来语和英语)上获得可读结果,但设备“A”在希伯来语的菱形内返回问号(编码问题)(英语工作正常)。

请指教(我需要做什么才能摆脱他们的钻石):)
我在下面添加了我使用的代码
埃拉德

public ArrayList<ItemSearch> getSuggestFromServer(String query,
        Context cntxt, int iCountResult)
{
    String savedQuery = String.copyValueOf(query.toCharArray());
    ArrayList<ItemSearch> mResultArrayList = new ArrayList<ItemSearch>();
    ArrayList<ItemSugges> mSuggesArrayList = new ArrayList<ItemSugges>();
    String mSuggestUri = null;
    String language = "";
    String country = "";
    if (mSuggestUri == null)
    {
        Locale l = Locale.getDefault();
        language = l.getLanguage();
        country = l.getCountry().toLowerCase();
        // Chinese and Portuguese have two langauge variants.
        if ("zh".equals(language))
        {
            if ("cn".equals(country))
            {
                language = "zh-CN";
            } else if ("tw".equals(country))
            {
                language = "zh-TW";
            }
        } else if ("pt".equals(language))
        {
            if ("br".equals(country))
            {
                language = "pt-BR";
            } else if ("pt".equals(country))
            {
                language = "pt-PT";
            }
        }
        mSuggestUri = cntxt.getResources().getString(
                "http://www.google.com/complete/search?hl=%1$s&amp;gl=%2$s&amp;", language, country)
                + "q=";
    }
    String resultString = new String("");
    try
    {
        query = URLEncoder.encode(query, "UTF-8");
        mSuggestUri += query + "&output=toolbar";

        URLConnection connection = null;
        // mSuggestUri
        URL url = new URL(mSuggestUri);
        connection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");

        String userAgent = System.getProperty("http.agent");
        httpConnection
                .setRequestProperty(
                        "User-Agent",
                        userAgent);

        httpConnection
                .setRequestProperty("Content-Type",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpConnection.setRequestProperty("Accept-Encoding", "deflate");

        httpConnection.setDoOutput(false);
        httpConnection.setDoInput(true);

        httpConnection.setConnectTimeout(2000);
        httpConnection.setReadTimeout(1000);


        // added
        httpConnection.setRequestProperty("http.keepAlive", "false");

        httpConnection.setRequestProperty("Connection", "close");
        // added

        httpConnection.connect();
        int responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK)
        {
            try
            {
                XmlPullParserFactory factory = XmlPullParserFactory
                        .newInstance();
                factory.setValidating(false);
                XmlPullParser mxml = factory.newPullParser();
                mxml.setInput(httpConnection.getInputStream(), null);
                int eventType = mxml.getEventType();
                int i = 0;
                while ((eventType != XmlPullParser.END_DOCUMENT))
                {
                    switch (eventType)
                    {
                        case XmlPullParser.START_DOCUMENT:

                            break;
                        case XmlPullParser.START_TAG:
                            String nameTag = mxml.getName();
                            if (nameTag.contains("suggestion"))
                            {
                                ItemSugges isuggest = new ItemSugges();
                                isuggest.sugges = mxml.getAttributeValue(
                                        null, "data");
                                mSuggesArrayList.add(isuggest);
                            } else if (nameTag.contains("num_queries"))
                            {
                                mSuggesArrayList.get(i).weigth = Long
                                        .valueOf(mxml.getAttributeValue(
                                                null, "int"));
                                i++;
                            }
                            break;
                        case XmlPullParser.TEXT:
                            String name = mxml.getText();
                            break;
                        case XmlPullParser.END_TAG:

                            break;
                        default:
                            break;
                    }
                    try
                    {
                        eventType = mxml.next();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }

                for (int indx = 0; mSuggesArrayList.size() > indx; indx++)
                {
                    ItemSearch is = new ItemSearch();
                    is.nameItem = mSuggesArrayList.get(indx).sugges;
                    is.spannerString = Mngr.getInstance().getSpannedString(
                            is.nameItem, savedQuery);
                    is.typeItem = typeItemSearch.web;
                    is.metaInfo = mSuggestUri = cntxt.getResources()
                            .getString(R.string.google_search_extended,
                                    language, country)
                            + "q=" + is.nameItem;

                    mResultArrayList.add(is);
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        } else
        {
            resultString = "Server does not respond";
        }
    } catch (MalformedURLException e)
    {
        resultString = "MalformedURLException:" + e.getMessage();
    } catch (IOException e)
    {
        Log.i("elad", "bummer", e);
        resultString = "IOException:" + e;//.getMessage();
    }
    return mResultArrayList;
}

【问题讨论】:

  • 我怀疑设备 A 不支持希伯来语。这不会让我感到惊讶。
  • 如果知道“设备 A”实际上是什么,就可以通过 Google 找到该信息。
  • @MichaelHampton 设备 A 是运行 Android 2.3.5 的三星 Galaxy S,第一个用户代理显示的是希伯来语结果,而第二个用户代理则不是。设备 B 是运行 Android 4.1 的三星 Galaxy S
  • @11684 设备 A 支持希伯来语阅读我上面的评论^^

标签: android html character-encoding


【解决方案1】:

找到解决方案,我找到了这个website
所以我决定使用下一个用户代理(硬编码)

Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3

没有问号。
它现在可以在两种设备上使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2012-04-10
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多