Android代码中实现WAP方式联网

无论是移动、联通还是电信,都至少提供了两种类型的的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类完整定义如下:

 
  1. package com.hust.iprai;

  2.  
  3. import android.content.ContentResolver;

  4. import android.content.Context;

  5. import android.database.Cursor;

  6. import android.net.ConnectivityManager;

  7. import android.net.NetworkInfo;

  8. import android.net.Uri;

  9.  
  10. public class APNManager {

  11.  
  12. public static final Uri PREFERRED_APN_URI;

  13.  
  14. private String mApn; // 接入点名称

  15.  
  16. private String mPort; // 端口号

  17.  
  18. private String mProxy; // 代理服务器

  19.  
  20. private boolean mUseWap; // 是否正在使用WAP

  21.  
  22. static {

  23. PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); // 取得当前设置的APN

  24. }

  25.  
  26. public APNManager(Context context) {

  27. checkNetworkType(context);

  28. }

  29.  
  30. /**

  31. * 获得当前设置的APN相关参数

  32. * @param context

  33. */

  34. private void checkApn(Context context) {

  35. ContentResolver contentResolver = context.getContentResolver();

  36. Uri uri = PREFERRED_APN_URI;

  37. String[] apnInfo = new String[3];

  38. apnInfo[0] = "apn";

  39. apnInfo[1] = "proxy";

  40. apnInfo[2] = "port";

  41.  
  42. Cursor cursor = contentResolver.query(uri, apnInfo, null, null, null);

  43. if (cursor != null) {

  44. while (cursor.moveToFirst()) {

  45. this.mApn = cursor.getString(cursor.getColumnIndex("apn"));

  46. this.mProxy = cursor.getString(cursor.getColumnIndex("proxy"));

  47. this.mPort = cursor.getString(cursor.getColumnIndex("port"));

  48.  
  49. // 代理为空

  50. if ((this.mProxy == null) || (this.mProxy.length() <= 0)) {

  51. String apn = this.mApn.toUpperCase();

  52.  
  53. // 中国移动WAP设置:APN:CMWAP;代理:10.0.0.172;端口:80

  54. // 中国联通WAP设置:APN:UNIWAP;代理:10.0.0.172;端口:80

  55. // 中国联通WAP设置(3G):APN:3GWAP;代理:10.0.0.172;端口:80

  56. if ((apn.equals("CMWAP")) || (apn.equals("UNIWAP")) || (apn.equals("3GWAP"))) {

  57. this.mUseWap = true;

  58. this.mProxy = "10.0.0.172";

  59. this.mPort = "80";

  60. break;

  61. }

  62.  
  63. // 中国电信WAP设置:APN(或者接入点名称):CTWAP;代理:10.0.0.200;端口:80

  64. if (apn.equals("CTWAP")) {

  65. this.mUseWap = true;

  66. this.mProxy = "10.0.0.200";

  67. this.mPort = "80";

  68. break;

  69. }

  70.  
  71. }

  72. this.mPort = "80";

  73. this.mUseWap = true;

  74. break;

  75. }

  76.  
  77. }

  78.  
  79. this.mUseWap = false;

  80. cursor.close();

  81. }

  82.  
  83. /**

  84. * 检测当前使用的网络类型是WIFI还是WAP

  85. * @param context

  86. */

  87. private void checkNetworkType(Context context) {

  88. NetworkInfo networkInfo = ((ConnectivityManager) context

  89. .getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

  90. if (networkInfo != null) {

  91. if (!"wifi".equals(networkInfo.getTypeName().toLowerCase())) {

  92. checkApn(context);

  93. return;

  94. }

  95. this.mUseWap = false;

  96. }

  97. }

  98.  
  99. /**

  100. * 判断当前网络连接状态

  101. * @param context

  102. * @return

  103. */

  104. public static boolean isNetworkConnected(Context context) {

  105. NetworkInfo networkInfo = ((ConnectivityManager) context

  106. .getApplicationContext().getSystemService("connectivity"))

  107. .getActiveNetworkInfo();

  108. if (networkInfo != null) {

  109. return networkInfo.isConnectedOrConnecting();

  110. }

  111. return false;

  112. }

  113.  
  114. public String getApn() {

  115. return this.mApn;

  116. }

  117.  
  118. public String getProxy() {

  119. return this.mProxy;

  120. }

  121.  
  122. public String getProxyPort() {

  123. return this.mPort;

  124. }

  125.  
  126. public boolean isWapNetwork() {

  127. return this.mUseWap;

  128. }

  129. }


ProxyHttpClient类完整定义如下:

 
  1. package com.hust.iprai;

  2.  
  3. import android.content.Context;

  4. import android.text.TextUtils;

  5. import android.util.Log;

  6.  
  7. import org.apache.http.HttpHost;

  8. import org.apache.http.conn.params.ConnRouteParams;

  9. import org.apache.http.impl.client.DefaultHttpClient;

  10. import org.apache.http.params.HttpConnectionParams;

  11. import org.apache.http.params.HttpParams;

  12. import org.apache.http.params.HttpProtocolParams;

  13.  
  14. public class ProxyHttpClient extends DefaultHttpClient {

  15.  
  16. private static final int HTTP_TIMEOUT_MS = 30 * 1000;

  17.  
  18. private static final int BUFFER_SIZE = 1024 * 8;

  19.  
  20. private static final String TAG = ProxyHttpClient.class.getSimpleName();

  21.  
  22. private RuntimeException mLeakedException = new IllegalStateException("ProxyHttpClient created and never closed");

  23.  
  24. private String mPort;

  25.  
  26. private String mProxy;

  27.  
  28. private boolean mUseWap;

  29.  
  30. public ProxyHttpClient(Context context) {

  31. this(context, null, null);

  32. }

  33.  
  34. public ProxyHttpClient(Context context, APNManager manager) {

  35. this(context, null, manager);

  36. }

  37.  
  38. public ProxyHttpClient(Context context, String userAgent) {

  39. this(context, userAgent, null);

  40. }

  41.  
  42. public ProxyHttpClient(Context context, String userAgent, APNManager manager) {

  43. if (manager == null) {

  44. manager = new APNManager(context);

  45. }

  46.  
  47. this.mUseWap = manager.isWapNetwork();

  48. this.mProxy = manager.getProxy();

  49. this.mPort = manager.getProxyPort();

  50. if (this.mUseWap) {

  51. HttpHost host = new HttpHost(this.mProxy, Integer.valueOf(this.mPort).intValue());

  52. getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, host); // 设置代理

  53. }

  54. HttpConnectionParams.setConnectionTimeout(getParams(), HTTP_TIMEOUT_MS);

  55. HttpConnectionParams.setSoTimeout(getParams(), HTTP_TIMEOUT_MS);

  56. HttpConnectionParams.setSocketBufferSize(getParams(), BUFFER_SIZE);

  57. if (!TextUtils.isEmpty(userAgent)) {

  58. HttpProtocolParams.setUserAgent(getParams(), userAgent);

  59. }

  60. }

  61.  
  62. public void close() {

  63. if (this.mLeakedException != null) {

  64. getConnectionManager().shutdown();

  65. this.mLeakedException = null;

  66. }

  67. }

  68.  
  69. protected HttpParams createHttpParams() {

  70. HttpParams params = super.createHttpParams();

  71. HttpProtocolParams.setUseExpectContinue(params, false);

  72. return params;

  73. }

  74.  
  75. protected void finalize() throws Throwable {

  76. super.finalize();

  77. if (this.mLeakedException != null) {

  78. Log.e(TAG, "Leak found", this.mLeakedException);

  79. }

  80. }

  81. }

如有什么建议和疑问,欢迎留言讨论。

欢迎大家加群技术交流:367685933

猜你喜欢

转载自blog.csdn.net/weixin_42600182/article/details/81264869
wap