解决Android下Wap模式无法访问网络的问题

http://blog.liaoxiaoqi.com/?p=563

经过测试发现,这个问题的产生是由于运营商(不管是移动、联通还是电信都一个德行。。)提供的wap方式访问网络需要通过代理网关,而我们在程序中使用的网络连接是http直连的方式,因此无法访问网络。解决这个问题的主要思路包括:

检测当前网络环境,判断网络状态在联网状态下,判断联网方式是Wifi或者GPRS在GPRS连接方式下,判断上网方式是net或是wap对于wap连接方式,在程序进行连接时,设置上相应的代理即可。

主要用到的代码如下:

 

////////////////////////////////////////////////////////////////////////////////

// 采用URLConnection的方式

////////////////////////////////////////////////////////////////////////////////

Proxy proxy;

boolean getSuccess = false;

URL url;

URLConnection urlConnection = null;

NetType netType = NetworkControl.getNetType(context);

InputStreamReader isr;

 

url = new URL(httpUrl);

// 判断Wap网络并设置代理

if (netType != null && netType.isWap()) {

proxy = new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(netType.getProxy(), netType.getPort()));

urlConnection = url.openConnection(proxy);

urlConnection.connect();

isr = new InputStreamReader(urlConnection.getInputStream(), "UTF-8");

} else {

isr = new InputStreamReader(url.openStream(), "GBK");

}

BufferedReader br = new BufferedReader(isr);

 

////////////////////////////////////////////////////////////////////////////////

// 采用HttpClient的方式

////////////////////////////////////////////////////////////////////////////////

HttpClient client = new DefaultHttpClient();

// 判断Wap网络并设置代理

NetType netType = NetworkControl.getNetType(context);

if (netType != null && netType.isWap()) {

HttpHost proxy = new HttpHost(netType.getProxy(), netType.getPort());

client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);

}

 

 

    public static class NetworkControl {

 

        public static boolean isNetworkAvailable(Context context){

            ConnectivityManager connectivityManager = (ConnectivityManager) 

                    context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo info = connectivityManager.getActiveNetworkInfo();

            return info != null;

        }

 

        public static NetType getNetworkType(Context context){

            NetType netType = new NetType();

 

            ConnectivityManager connectivityManager = (ConnectivityManager) 

                    context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo info = connectivityManager.getActiveNetworkInfo();

            if(info == null)

                return netType;

 

            String type = info.getTypeName();

            if (type.equalsIgnoreCase("WIFI")) {

                netType.setType(NetType.NET_TYPE_WIFI);

            } else if(type.equalsIgnoreCase("MOBILE")) {

                // GPRS

                netType.setType(NetType.NET_TYPE_MOBILE_NET);

                String proxyHost = android.net.Proxy.getDefaultHost();

                if (!TextUtils.isEmpty(proxyHost)) {

                    // WAP

                    netType.setType(NetType.NET_TYPE_MOBILE_WAP);

                    netType.setProxy(proxyHost);

                    netType.setPort(android.net.Proxy.getDefaultPort());

                }

            }

            return netType;

        }

 

        public static class NetType {

            public static final int NET_TYPE_UNAVAILABLE = 0;

            public static final int NET_TYPE_WIFI = 1;

            public static final int NET_TYPE_MOBILE_NET = 2;

            public static final int NET_TYPE_MOBILE_WAP = 3;

            

            private int type = NET_TYPE_UNAVAILABLE;

            private String proxy = null;

            private int port = 0;

            

            public int getType() {

                return type;

            }

            public void setType(int type) {

                this.type = type;

            }

            public String getProxy() {

                return proxy;

            }

            public void setProxy(String proxy) {

                this.proxy = proxy;

            }

            public int getPort() {

                return port;

            }

            public void setPort(int port) {

                this.port = port;

            }

        }

    }


 

 

这个方法基本可以解决移动、联通和电信三大运营商下的wap连接问题,包括双卡双待手机(在XT800上试验可行)。不过还存在一点问题就是,wap连接方式不够稳定,偶尔还是会出现连接不上的情况,需要重新操作才能连上。目前没有比较好的解决方法,以后有时间继续研究。