2012-09-14 42 views
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」的回報問號裏面的鑽石(編碼問題)爲希伯來語(英語工作正常)。

請指教(我需要做什麼做擺脫「時間鑽石):)
我已經添加了我使用下面
埃拉德代碼

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; 
} 
+0

我懷疑設備A只是不支持希伯來語。這不會讓我感到驚訝。 – 11684

+0

如果有人知道「設備A」實際上是什麼,可以通過Google找到該信息。 –

+0

@MichaelHampton Device A是運行Android 2.3.5的三星Galaxy S,第一個用戶代理顯示希伯來語結果,第二個用戶代理不顯示希伯來語結果。 設備B是運行Android 4.1的Samsung Galaxy S –

回答

1

發現的解決方案,我發現這website
所以我決定使用下一個用戶代理(硬編碼)

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

沒有問號。
它現在可以在兩臺設備上運行。