在有中国特色的 GPRS 接入方式背景下,时至今日,我们仍然不得不考虑 cmwap 用户上网的问题;虽然我们暂时找不到关于统计cmwap用户的确切使用比例,但是可以确定的是一些做的比较好的 Android 手机应用绝对不会出现换了 cmwap 就无法网络通信的问题,要解决这个问题其实很简单,考虑两个步骤:1、检测用户手机的设置是否打开了 cmwap。2、使用 Android HttpClient 的代理模式上 cmwap。具体的实现代码如下,希望对有需要的朋友有所帮助。
1、以下是检测手机上网类型的工具类:
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
public class HttpUtil {
static public int WAP_INT = 1;
static public int NET_INT = 2;
static public int WIFI_INT = 3;
static public int NONET_INT = 4;
static private Uri APN_URI = null;
static public int getNetType (Context ctx) {
// has network
ConnectivityManager conn = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conn == null) {
return HttpUtil.NONET_INT;
}
NetworkInfo info = conn.getActiveNetworkInfo();
if (info == null){
return HttpUtil.NONET_INT;
}
// check use wifi
String type = info.getTypeName();
if (type.equals("WIFI")) {
return HttpUtil.WIFI_INT;
}
// check use wap
APN_URI = Uri.parse("content://telephony/carriers/preferapn");
Cursor uriCursor = ctx.getContentResolver().query(APN_URI, null, null, null, null);
if (uriCursor != null && uriCursor.moveToFirst()) {
String proxy = uriCursor.getString(uriCursor.getColumnIndex("proxy"));
String port = uriCursor.getString(uriCursor.getColumnIndex("port"));
String apn = uriCursor.getString(uriCursor.getColumnIndex("apn"));
if (proxy != null && port != null && apn != null && apn.equals("cmwap") && port.equals("80") &&
(proxy.equals("10.0.0.172") || proxy.equals("010.000.000.172"))) {
return HttpUtil.WAP_INT;
}
}
return HttpUtil.NET_INT;
}
}
2、以下是我自己封装 HttpClient 的 AppClient 类的部分代码:
... ...
public class AppClient {
private String apiUrl;
private HttpParams httpParams;
private HttpClient httpClient;
... ...
public AppClient (String url) {
initClient(url);
}
... ...
private void initClient (String url) {
// initialize API URL
this.apiUrl = url;
// set client timeout
httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
// init client
httpClient = new DefaultHttpClient(httpParams);
}
public void useWap () {
HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
public String get () throws Exception {
try {
HttpGet httpGet = headerFilter(new HttpGet(this.apiUrl));
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String httpResult = resultFilter(httpResponse.getEntity());
return httpResult;
} else {
return null;
}
} catch (ConnectTimeoutException e) {
throw new Exception(C.err.network);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
... ...
}
使用的时候,先用 HttpUtil.getNetType() 获取用户上网方式,如果是 HttpUtil.WAP_INT 的话,则在使用 AppClient 的时候多调用一个 useWap() 方法。示例代码如下:
... ...
AppClient ac = new AppClient(url);
if (HttpUtil.WAP_INT == HttpUtil.getNetType(this)) {
ac.useWap();
}
... ...