My application uses the following codes to connect to web site and download a web page for processing.
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("User-Agent", 开发者_JS百科"Mozilla/3.0 (compatible; MSIE 4.0; Windows NT)");
c.setRequestProperty("Accept-Language", "zh-hk");
c.setRequestProperty("Connection", "Keep-Alive");
c.setConnectTimeout(6000); // mTimeout is in seconds
intTries = 3;
do {
c.connect();
if (c.getResponseCode() == 200) {
break;
}
else {
intTries --;
}
} while (intTries > 0);
The codes are run in main thread only. If WiFi is being used, it is nice and clean. If 3G connection is being used, sometimes, it cannot get connected OR application HANGS.
Is there any significant difference between handling WiFi and 3G ?
Thanks.
The codes are run in main thread only.
That is not a good idea. Please use an AsyncTask
or something else to get that logic off of the main thread. You might also consider using HttpClient instead of HttpUrlConnection
, since that is built into Android as well.
You might also wish to test your URL with the built-in Browser application. If you get similar results, then there is something going on between your wireless carrier and the site you are trying to reach.
精彩评论