Android 获取联网方式及代理联网

得到联网方式的方法
public String NetType(Context context) { 
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
String typeName = info.getTypeName().toLowerCase; // WIFI/MOBILE
if(typeName.equals.("wifi")){
}else{
typeName = mActiveNetworkInfo.getExtraInfo().toLowerCase();
//3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap
}
return typeName;
} catch (Exception e) {
return null;
}
}


没有网络时会出现异常,位置为ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();


使用代理联网时得到连接对象的方法

private HttpURLConnection getURLConnection(String url) throws Exception {
String proxyHost = android.net.Proxy.getDefaultHost();
if (proxyHost != null) {
java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress(android.net.Proxy.getDefaultHost(),
android.net.Proxy.getDefaultPort()));

return (HttpURLConnection) new URL(url).openConnection(p);

} else {
return (HttpURLConnection) new URL(url).openConnection();
}
}


返回HttpURLConnection对象android.net.Proxy.getDefaultHost()得到手机设置的代理ip,得到android.net.Proxy.getDefaultPort()得到手机设置的端口;
也可以自己设置为 10.0.0.172 端口 80
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值