次のエンコーディングの問題が発生しました:
最初に Google にクエリを実行したときに、ユーザー エージェントがハードコードされていました: のように
"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&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;
}