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&gl=%2$s&", 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가 히브리어를 지원하지 않는다고 생각합니다. 그것은 놀랄 일이 아닙니다. – 11684
"기기 A"가 실제로 무엇인지 알면 Google을 통해 해당 정보를 찾을 수 있습니다. –
@MichaelHampton Device A는 Android 2.3.5를 실행하는 Samsung Galaxy S로 히브리어 결과를 표시하는 첫 번째 사용자 에이전트가 있으며 두 번째 사용자 에이전트는 그렇지 않습니다. 기기 B는 삼성 갤럭시 S입니다. Android 4.1을 실행 중입니다. –