Android : set httpclient timeout in android

on Sunday, September 14, 2014


First it system.out "network found" ,and at the end I see no network.“code” doesn't come out public class NetUtil { public static boolean checkNet(Context context) {



ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (info != null && info.isConnected()) {
System.out.println("network found");
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
httpclient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 1000);

HttpGet httpGet = new HttpGet("http://www.baidu.com");

try {
HttpResponse response = httpclient.execute(httpGet);

int code = response.getStatusLine().getStatusCode();

System.out.println("code:" + code);

if (code == 200) {
System.out.println("success");
return true;
} else if (code != 200) {
System.out.println("failed");
return false;
}

} catch (Exception e) {
// TODO: handle exception
}

}

System.out.println("no network");
return false;

}


}


0 comments:

Post a Comment