当前位置: 代码迷 >> Android >> android 创办兼容CMWAP连接的应用程序
  详细解决方案

android 创办兼容CMWAP连接的应用程序

热度:20   发布时间:2016-05-01 15:28:05.0
android 创建兼容CMWAP连接的应用程序

在Android开发中,经常会用到连接网络的操作,比如下载指定位置的网络图片,根据用户网络连接APN设置不同,我们在编程的时候要进行判断,做到网络的兼容,即无论用户选择的是CMNET还是CMWAP抑或是3G上网,我们的软件都要达到正常运行的程度。

要做到这一点,首先要判断用户当前使用的接入点类型,以下方法可以获取Android系统的手机当前使用的接入点类型。

先声明一个枚举,区分接入点

/*
* 接入点类型
*/

?

public enum APNType {   CMWAP, CMNET, Unknow}

?

然后通过下面的函数获取接入点类型

?

public APNType getCurrentUsedAPNType() {   try {    ContentResolver cr = getContentResolver();    Cursor cursor = cr.query(PREFERRED_APN_URI,      new String[] { "_id", "apn", "type" }, null, null, null);    cursor.moveToFirst();    if (cursor.isAfterLast()) {     return APNType.Unknow;    }    //long id = cursor.getLong(0);    String apn = cursor.getString(1);    //String type = cursor.getString(2);    if (apn.toUpperCase().equals("CMWAP"))     return APNType.CMWAP;    else if (apn.toUpperCase().equals("CMWNET"))     return APNType.CMNET;    else     return APNType.Unknow;   } catch (Exception ep) {    Log.e("getCurrentUsedAPNType", ep.getMessage());    return APNType.Unknow;   }}

?

?

获取完接入点类型后,如果使用的是CMWAP,那么我们在进行网络连接的时候,要使用移动梦网代理服务器,10.0.0.172:80,具体代码如下

?

public String getHtmlCode(String httpUrl) {   String htmlCode = "";   try {    InputStream in;     String urlt=httpUrl;    String pUrl = "http://10.0.0.172";    urlt=urlt.replace("http://", "");    pUrl=pUrl+urlt.substring(urlt.indexOf("/"));    urlt=urlt.substring(0,urlt.indexOf("/"));       URL url = new java.net.URL(pUrl);    HttpURLConnection connection = (HttpURLConnection) url      .openConnection();    connection = (HttpURLConnection) url.openConnection();    // connection.setRequestProperty("User-Agent", "Mozilla/4.0");       connection.setRequestProperty("X-Online-Host", urlt);       connection.setDoInput(true);        connection.connect();    in = connection.getInputStream();    byte[] buffer = new byte[512];    int length = -1;    int sep = 0;    this.smallProgressBar.setProgress(sep);    this.smallProgressBar.setMax(204800);    while ((length = in.read(buffer, 0, 512)) != -1) {     sep += length;     if (sep > 204800)      sep = 204800;     this.smallProgressBar.setProgress(sep);     htmlCode += new String(buffer, 0, length, "gb2312");    }    in.close();    connection.disconnect();       System.gc();   } catch (Exception e) {    Log.e("getHtmlCode", e.getMessage());   }   if (htmlCode == null) {    return "";   }   return htmlCode;}

?需要注意的是,通过移动梦网连接网络,移动梦网可能要进行一次URL转向,相应的在逻辑中要进行二次请求,在这里就不再进行代码说明了。

?

  相关解决方案