正文  网络通信 > HTTP >

Android判断网络连接

以HttpClient连接为例:HttpClient httpClient = new DefaultHttpClient(); ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); ......

以HttpClient连接为例:

HttpClient httpClient = new DefaultHttpClient();
            ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo networkinfo = manager.getActiveNetworkInfo();
            String net = networkinfo.getExtraInfo();//获取网络类型
            if (net != null)//wifi的值为空,这里判断下
            {
                if (net.equals("cmwap"))
                {
                    HttpHost proxy = new HttpHost("10.0.0.172", 80);//设置cmwap代理
                    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                }
            }
            //HttpGet .....

            记得加上这个权限  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>