Android中使用cmwap接入点访问互联网的问题及解决办法

01//检查网络 是否正常
02    private boolean checkNet(){
03     
04ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);  
05     
06netWrokInfo = manager.getActiveNetworkInfo();  
07       if (netWrokInfo == null || !netWrokInfo.isAvailable()) { 
08           Toast.makeText(this, "当前的网络不可用,请开启\n网络", Toast.LENGTH_LONG).show();
09           return false;
10       }
11       else if(netWrokInfo.getTypeName().equals("MOBILE")& netWrokInfo.getExt raInfo().equals("cmwap")){
12           Toast.makeText(this, "cmwap网络不可用,请选择cmnet网络", Toast.LENGTH_LONG).show();
13           return false;
14       }else{
15         
16return true;
17       }
18    }

/** 

*Android 使用cmwap GPRS 方式联网 

*/ 

CMWAP和CMNET只是中国移动为其划分的两个GPRS接入方式。中国移动对CMWAP作了一定的限制,主要表现在CMWAP接入时只能访问 GPRS网络内的IP(10.*.*.*),而无法通过路由访问Internet,我们用CMWAP浏览Internet上的网页 就是通过WAP网关协议或它提供的HTTP代理服务实现的。 因此,只有满足以下两个条件的应用 才能在中国移动的CMWAP接入方式下正常工作: 

1.应用程序 的网络请求基于HTTP协议。 

2.应用程序 支持HTTP代理协议或WAP网关协议。 

这也就是为什么我们的G1无法正常用CMWAP的原因。 

一句话:CMWAP是移动限制的,理论上只能上WAP网,而CMNET可以用GPRS浏览WWW 

方法一: 

1URL url = new URL("http://10.0.0.172/img/baidu_logo.gif");  
2HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
3conn.setRequestProperty("X-Online-Host", "www.baidu.com");  
4conn.setDoInput(true);  
5conn.connect();  
6InputStream is = conn.getInputStream();  
7bitmap = BitmapFactory.decodeStream(is);  
8is.close();  
9conn.disconnect();

方法二: 

CODE: 

01package org.apache.http.examp les.client;
02  
03import org.apache.http.Header;
04import org.apache.http.HttpEntity;
05import org.apache.http.HttpHost;
06import org.apache.http.HttpResponse;
07import org.apache.http.client.HttpClient;
08import org.apache.http.client.methods.HttpGet;
09import org.apache.http.conn.params.ConnRoutePNames;
10import org.apache.http.impl.client.DefaultHttpClient;
11import org.apache.http.util.EntityUtils;
12  
13public class ClientExecuteProxy {
14  
15    public static void main(String [] args)throws Exception {
16  
17        HttpHost proxy = new HttpHost( "10.0.0.172", 80, "http");
18        HttpHost target = new HttpHost("YOUR_TARGET_IP", 80, "http");        
19  
20        DefaultHttpClient httpclient = new DefaultHttpClient();
21        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
22  
23          
24        HttpGet req = new HttpGet("/");
25  
26        System.out.println("executing request to " + target + " via " + proxy);
27        HttpResponse rsp = httpclient.execute(target, req);
28        HttpEntity entity = rsp.getEntity();
29  
30        System.out.println("----------------------------------------");
31        System.out.println(rsp.getStatusLine());
32        Header[] headers = rsp.getAllHeaders();
33        for (int i = 0; i<headers.length; i++) {
34            System.out.println(headers);
35        }
36        System.out.println("----------------------------------------");
37  
38        if (entity != null) {
39            System.out.println(EntityUtils.toString(entity));
40        }
41  
42        // When HttpClient instance is no longer needed, 
43        // shut down the connection manager to ensure
44        // immediate deallocation of all system resources
45        httpclient.getConnectionManager().shutdown();        
46    }
47  
48}

在Android上建立GPRS连接 

01private boolean openDataConnection() {
02    // Set up data connection.
03    DataConnection conn = DataConnection.getInstance();        
04 
05        if (connectMode == 0) {
06            ret = conn.openConnection(mContext, "cmwap", "cmwap", "cmwap");
07        } else {
08            ret = conn.openConnection(mContext, "cmnet", "", "");
09        }
10 
11}<SPAN style="FONT-FAMILY: ''sans serif', tahoma, verdana, helvetica'; FONT-SIZE: x-small"><SPAN style="LINE-HEIGHT: 19px; WHITE-SPACE: normal"> </SPAN></SPAN>

Android 判断网络状态 

在使用Android连接网络的时候,并不是每次都能连接到网络,在这个时候,我们最好是在程序启动的时候对网络的状态进行一下判断,如果没有网络则进行即时提醒用户进行设置。 

要判断网络状态,首先需要有相应的权限,下面为权限代码: 

即允许访问网络状态: 

1<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

下面为判断代码: 

01private boolean NetWorkStatus() {
02  
03boolean netSataus = false;
04        ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
05  
06        cwjManager.getActiveNetworkInfo();
07  
08if (cwjManager.getActiveNetworkInfo() != null) {
09            netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
10        }
11  
12if (netSataus) {
13            Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络")
14                    .setMessage("是否对网络进行设置?");
15            b.setPositiveButton("是", new DialogInterface.OnClickListener() {
16public void onClick(DialogInterface dialog, int whichButton) {
17                    Intent mIntent = new Intent("/");
18                    ComponentName comp = new ComponentName(
19"com.android.settings",
20"com.android.settings.WirelessSettings");
21                    mIntent.setComponent(comp);
22                    mIntent.setAction("android.intent.action.VIEW");
23                    startActivityForResult(mIntent,0);  // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写
24                }
25            }).setNeutralButton("否", new DialogInterface.OnClickListener() {
26public void onClick(DialogInterface dialog, int whichButton) {
27                    dialog.cancel();
28                }
29            }).show();
30        }
31  
32return netSataus;
33    }
34//通过上面的代码即可完成对网络状态的判断!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值