转载地址:http://www.linuxidc.com/Linux/2012-08/67980.htm
学习中,用到的,记录下。
无论是移动、联通还是电信,都至少提供了两种类型的的APN:WAP方式和NET方式。其中NET方式跟WIFI方式一样,无需任何设置,可自由访问所有类型网站,而WAP方式,需要手机先设置代理服务器和端口号等信息,并且只能访问HTTP协议类型的网站。
1) 移动的WAP名称是CMWAP,NET名称是CMNET;
2) 联通的WAP名称是UNIWAP,NET名称是UNINET;联通3G的WAP名称是3GWAP,NET名称是3GNET;
3) 电信的WAP名称是CTWAP,NET名称是CTNET;
其中,移动和联通的WAP代理服务器都是10.0.0.172,端口号是80;而电信的WAP代理服务器是10.0.0.200,端口号是80。
Android系统中,对于APN网络的API是隐藏的,因此获取手机的APN设置,需要通过ContentProvider来进行数据库查询,查询的URI地址是:
取得全部的APN列表:content://telephony/carriers;
取得当前设置的APN:content://telephony/carriers/preferapn;
取得current=1的APN:content://telephony/carriers/current;
下面我们的代码就是获取当前首选的APN设置,并继承HttpClient,实现我们自己的代理HttpClient类。首先来看下APN的管理类的实现,这个类的主要功能是获得APN的代理服务器和端口号,查询用的URI如下:
由这个URI使用ContentResolver获得游标对象,之后就是查询操作了,分别查处当前手机所设置的APN、Proxy和Port,而如果手机的Proxy没有设置,则需要根据APN来决定当前应该连接的代理服务器地址和端口号,详细代码如下所示:
通过APNManager类获取到当前手机的WAP设置的代理和端口之后,就可以构造我们自己的代理HttpClient了,这个类定义为ProxyHttpClient,在该类的构造函数中,首先获得APNManager的实例,然后获取代理服务器proxy和端口值port,通过这两个参数构造HttpHost实例,并将host实例设置为ConnRouteParams.DEFAULT_PROXY的值,详细代码截图如下所示:
APNManager类完整定义如下:
- package com.hust.iprai;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.database.Cursor;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.Uri;
- public class APNManager {
- public static final Uri PREFERRED_APN_URI;
- private String mApn; // 接入点名称
- private String mPort; // 端口号
- private String mProxy; // 代理服务器
- private boolean mUseWap; // 是否正在使用WAP
- static {
- PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); // 取得当前设置的APN
- }
- public APNManager(Context context) {
- checkNetworkType(context);
- }
- /**
- * 获得当前设置的APN相关参数
- * @param context
- */
- private void checkApn(Context context) {
- ContentResolver contentResolver = context.getContentResolver();
- Uri uri = PREFERRED_APN_URI;
- String[] apnInfo = new String[3];
- apnInfo[0] = "apn";
- apnInfo[1] = "proxy";
- apnInfo[2] = "port";
- Cursor cursor = contentResolver.query(uri, apnInfo, null, null, null);
- if (cursor != null) {
- while (cursor.moveToFirst()) {
- this.mApn = cursor.getString(cursor.getColumnIndex("apn"));
- this.mProxy = cursor.getString(cursor.getColumnIndex("proxy"));
- this.mPort = cursor.getString(cursor.getColumnIndex("port"));
- // 代理为空
- if ((this.mProxy == null) || (this.mProxy.length() <= 0)) {
- String apn = this.mApn.toUpperCase();
- // 中国移动WAP设置:APN:CMWAP;代理:10.0.0.172;端口:80
- // 中国联通WAP设置:APN:UNIWAP;代理:10.0.0.172;端口:80
- // 中国联通WAP设置(3G):APN:3GWAP;代理:10.0.0.172;端口:80
- if ((apn.equals("CMWAP")) || (apn.equals("UNIWAP")) || (apn.equals("3GWAP"))) {
- this.mUseWap = true;
- this.mProxy = "10.0.0.172";
- this.mPort = "80";
- break;
- }
- // 中国电信WAP设置:APN(或者接入点名称):CTWAP;代理:10.0.0.200;端口:80
- if (apn.equals("CTWAP")) {
- this.mUseWap = true;
- this.mProxy = "10.0.0.200";
- this.mPort = "80";
- break;
- }
- }
- this.mPort = "80";
- this.mUseWap = true;
- break;
- }
- }
- this.mUseWap = false;
- cursor.close();
- }
- /**
- * 检测当前使用的网络类型是WIFI还是WAP
- * @param context
- */
- private void checkNetworkType(Context context) {
- NetworkInfo networkInfo = ((ConnectivityManager) context
- .getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
- if (networkInfo != null) {
- if (!"wifi".equals(networkInfo.getTypeName().toLowerCase())) {
- checkApn(context);
- return;
- }
- this.mUseWap = false;
- }
- }
- /**
- * 判断当前网络连接状态
- * @param context
- * @return
- */
- public static boolean isNetworkConnected(Context context) {
- NetworkInfo networkInfo = ((ConnectivityManager) context
- .getApplicationContext().getSystemService("connectivity"))
- .getActiveNetworkInfo();
- if (networkInfo != null) {
- return networkInfo.isConnectedOrConnecting();
- }
- return false;
- }
- public String getApn() {
- return this.mApn;
- }
- public String getProxy() {
- return this.mProxy;
- }
- public String getProxyPort() {
- return this.mPort;
- }
- public boolean isWapNetwork() {
- return this.mUseWap;
- }
- }
ProxyHttpClient类完整定义如下:
- package com.hust.iprai;
- import android.content.Context;
- import android.text.TextUtils;
- import android.util.Log;
- import org.apache.http.HttpHost;
- import org.apache.http.conn.params.ConnRouteParams;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.HttpConnectionParams;
- import org.apache.http.params.HttpParams;
- import org.apache.http.params.HttpProtocolParams;
- public class ProxyHttpClient extends DefaultHttpClient {
- private static final int HTTP_TIMEOUT_MS = 30 * 1000;
- private static final int BUFFER_SIZE = 1024 * 8;
- private static final String TAG = ProxyHttpClient.class.getSimpleName();
- private RuntimeException mLeakedException = new IllegalStateException("ProxyHttpClient created and never closed");
- private String mPort;
- private String mProxy;
- private boolean mUseWap;
- public ProxyHttpClient(Context context) {
- this(context, null, null);
- }
- public ProxyHttpClient(Context context, APNManager manager) {
- this(context, null, manager);
- }
- public ProxyHttpClient(Context context, String userAgent) {
- this(context, userAgent, null);
- }
- public ProxyHttpClient(Context context, String userAgent, APNManager manager) {
- if (manager == null) {
- manager = new APNManager(context);
- }
- this.mUseWap = manager.isWapNetwork();
- this.mProxy = manager.getProxy();
- this.mPort = manager.getProxyPort();
- if (this.mUseWap) {
- HttpHost host = new HttpHost(this.mProxy, Integer.valueOf(this.mPort).intValue());
- getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, host); // 设置代理
- }
- HttpConnectionParams.setConnectionTimeout(getParams(), HTTP_TIMEOUT_MS);
- HttpConnectionParams.setSoTimeout(getParams(), HTTP_TIMEOUT_MS);
- HttpConnectionParams.setSocketBufferSize(getParams(), BUFFER_SIZE);
- if (!TextUtils.isEmpty(userAgent)) {
- HttpProtocolParams.setUserAgent(getParams(), userAgent);
- }
- }
- public void close() {
- if (this.mLeakedException != null) {
- getConnectionManager().shutdown();
- this.mLeakedException = null;
- }
- }
- protected HttpParams createHttpParams() {
- HttpParams params = super.createHttpParams();
- HttpProtocolParams.setUseExpectContinue(params, false);
- return params;
- }
- protected void finalize() throws Throwable {
- super.finalize();
- if (this.mLeakedException != null) {
- Log.e(TAG, "Leak found", this.mLeakedException);
- }
- }
- }