1

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

"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”在希伯来语的菱形内返回问号(编码问题)(英语工作正常)。

请告知(我需要做什么才能摆脱他们的钻石):)
我已经在
Elad下面添加了我使用的代码

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;
}
4

1 回答 1

1

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

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

没有问号。
它现在适用于两种设备。

于 2012-09-15T08:56:21.607 回答