2012-09-14 4 views
1

결과 : 먼저 사용자 에이전트에
구글 쿼리 할 때 하드 코딩 된 : 그래서
다이아몬드 내가 다음 인코딩 문제가 발생했습니다

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

I가 유효한 결과를받는 것처럼 장치 "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를 실행하는 Samsung Galaxy S로 히브리어 결과를 표시하는 첫 번째 사용자 에이전트가 있으며 두 번째 사용자 에이전트는 그렇지 않습니다. 기기 B는 삼성 갤럭시 S입니다. Android 4.1을 실행 중입니다. –

답변

1

해결책을 발견, 내가 이걸 발견 한 나는
Elad 이하로 사용하는 코드를 추가했습니다 :)
(나는 그들을 다이아몬드 '를 제거하려면 어떻게해야합니까 무엇) website
그래서 나는 다음 사용자 에이전트를 사용하기로 결정했습니다는

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

에는 물음표 (하드 코딩 없음).
이제 두 장치 모두에서 작동합니다.

관련 문제