Reputation: 1182
I've encountered the next Encoding problem: when querying google
at first the user agent was hardcoded: like so
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0"
I was receiving valid results on device "A" , But on Device "B" the method:
httpConnection.getResponseCode();
returned time out exception
.
I then decided to try some voodoo, I changed "user agent" to:
String userAgent = System.getProperty("http.agent");
now I get readable results on device "B"(Hebrew and English), but device "A" returns question marks inside diamonds(encoding problem) for the Hebrew language(English works fine).
please advise (What do I need to do to get rid of 'em diamonds) :)
I've added the code I use below
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;
}
Upvotes: 1
Views: 663
Reputation: 1182
Solution found, I have found this website
And so I've decided to use the next user agent (hard coded)
Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3
no question marks.
it works on both devices now.
Upvotes: 1