您的位置:首页 > 业界新闻

Android 使用cmwap访问互联网的办法

2012-09-13 14:25 429 查看
Android使用cmwap访问互联网的办法

原文链接:http://www.oschina.net/code/snippet_4873_4914

[代码][Java]代码

01
//检查网络
是否正常
02
private
boolean
checkNet(){
03
04
ConnectivityManager
manager=(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
05
06
netWrokInfo
=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.getExtraInfo().equals(
"cmwap"
)){
12
Toast.makeText(
this
,
"cmwap网络不可用,请选择cmnet网络"
,
Toast.LENGTH_LONG).show();
13
return
false
;
14
}
else
{
15
16
return
true
;
17
}
18
}

[代码][Java]代码

01
/**
02
Android
使用cmwapGPRS方式联网
03
CMWAP和CMNET只是中国移动为其划分的两个GPRS接入方式。中国移动对CMWAP作了一定的限制,主要表现在CMWAP接入时只能访问
GPRS网络内的IP(10.*.*.*),而无法通过路由访问Internet,我们用CMWAP浏览Internet上的网页就是通过WAP网关协议或它提供的HTTP代理服务实现的。因此,只有满足以下两个条件的应用才能在中国移动的CMWAP接入方式下正常工作:
04
1.应用程序
的网络请求基于HTTP协议。
05
2.应用程序
支持HTTP代理协议或WAP网关协议。
06
这也就是为什么我们的G1无法正常用CMWAP的原因。
07
一句话:CMWAP是移动限制的,理论上只能上WAP网,而CMNET可以用GPRS浏览WWW
08
方法一:
09
*/
10
URL
url=
new
URL(
"http://10.0.0.172/img/baidu_logo.gif"
);
11
HttpURLConnection
conn=(HttpURLConnection)url.openConnection();
12
conn.setRequestProperty(
"X-Online-Host"
,
"www.baidu.com"
);
13
conn.setDoInput(
true
);
14
conn.connect();
15
InputStream
is=conn.getInputStream();
16
bitmap
=BitmapFactory.decodeStream(is);
17
is.close();
18
conn.disconnect();

[代码][Java]代码

01
package
org.apache.http.examp
les.client;
02
03
import
org.apache.http.Header;
04
import
org.apache.http.HttpEntity;
05
import
org.apache.http.HttpHost;
06
import
org.apache.http.HttpResponse;
07
import
org.apache.http.client.HttpClient;
08
import
org.apache.http.client.methods.HttpGet;
09
import
org.apache.http.conn.params.ConnRoutePNames;
10
import
org.apache.http.impl.client.DefaultHttpClient;
11
import
org.apache.http.util.EntityUtils;
12
13
public
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
requestto"
+
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
//
WhenHttpClientinstanceisnolongerneeded,
43
//
shutdowntheconnectionmanagertoensure
44
//
immediatedeallocationofallsystemresources
45
httpclient.getConnectionManager().shutdown();
46
}
47
48
}

[代码]在Android上建立GPRS连接

01
private
boolean
openDataConnection()
{
02
//
Setupdataconnection.
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
}

[代码]Android判断网络状态

01
/*
02
在使用Android连接网络的时候,并不是每次都能连接到网络,在这个时候,我们最好是在程序启动的时候对网络的状态进行一下判断,如果没有网络则进行即时提醒用户进行设置。
03
要判断网络状态,首先需要有相应的权限,下面为权限代码:
04
即允许访问网络状态:
05
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
06
下面为判断代码:
07
*/
08
private
boolean
NetWorkStatus()
{
09
10
boolean
netSataus
=
false
;
11
ConnectivityManager
cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
12
13
cwjManager.getActiveNetworkInfo();
14
15
if
(cwjManager.getActiveNetworkInfo()
!=
null
)
{
16
netSataus
=cwjManager.getActiveNetworkInfo().isAvailable();
17
}
18
19
if
(netSataus)
{
20
Builder
b=
new
AlertDialog.Builder(
this
).setTitle(
"没有可用的网络"
)
21
.setMessage(
"是否对网络进行设置?"
);
22
b.setPositiveButton(
"是"
,
new
DialogInterface.OnClickListener()
{
23
public
void
onClick(DialogInterface
dialog,
int
whichButton)
{
24
Intent
mIntent=
new
Intent(
"/"
);
25
ComponentName
comp=
new
ComponentName(
26
"com.android.settings"
,
27
"com.android.settings.WirelessSettings"
);
28
mIntent.setComponent(comp);
29
mIntent.setAction(
"android.intent.action.VIEW"
);
30
startActivityForResult(mIntent,
0
);
//
如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写
31
}
32
}).setNeutralButton(
"否"
,
new
DialogInterface.OnClickListener()
{
33
public
void
onClick(DialogInterface
dialog,
int
whichButton)
{
34
dialog.cancel();
35
}
36
}).show();
37
}
38
39
return
netSataus;
40
}
41
//通过上面的代码即可完成对网络状态的判断!
移动电信联通APNcmwapcmnetctwapctnet3gwapuniwap3gnetuninet是什么怎么设置

 APN(AccessPointName),即“接入点名称”,用来标识GPRS的业务种类,目前分为两大类:CMWAP(通过GPRS访问WAP业务)、CMNET(除了WAP以外的服务目前都用CMNET,比如连接因特网等)。

中国移动APN:cmwap(2G)cmnet(3G)

中国电信APN:ctwap(2G)ctnet(3G)

中国联通APN:uniwap(2G手机)3gwap(3G手机)uninet(2G电脑)3gnet(3G电脑)

一般wap接入和上飞信或者手机登陆运营商营业厅用wap(2G)

一般上网用户会选择net

wifi是个人无线热点通过宽带或者光纤加上无线路由可以建立30m以上的无线热点,速率有你的宽带决定,也是最便宜的但是不能随身携带只有在家中公司咖啡厅可以使用。同时需要带wifi的终端支持耗电少

一般手机GPRS,NET,彩信的设置:

中国移动

1、

名称:CMNET

APN:CMNET

代理:空

端口:空

用户名:空

密码:空

服务器:空

MMSC:空

彩信代理:空

彩信端口:空

彩信协议:WAP2.0

MCC:460

MNC:00

APN类型:default

MENU→保存

2、

名称:CMWAP

APN:CMWAP

代理:010.000.000.172

端口:80

用户名:空

密码:空

服务器:空

MMSC:空

彩信代理:空

彩信端口:空

彩信协议:WAP2.0

MCC:460

MNC:00

APN类型:default

MENU→保存

3、

名称:CMMMS

APN:CMWAP

代理:010.000.000.172

端口:80

用户名:空

密码:空

服务器:空

MMSC:http//mmsc.monternet.com

彩信代理:010.000.000.172

彩信端口:80

彩信协议:WAP2.0

MCC:460

MNC:00

APN类型:mms

MENU→保存

4、选择CMNET、CMWAP、CMMMS中的CMNET,网络运营商选择CMCC,启用始终连接移动数据

中国联通

1、

名称:WCDMA(名称可自定义,3G的WCDMA是3GNET)

APN:UNINET(3G的WCDMA是3GNET)

代理:空

端口:空

用户名:空

密码:空

服务器:空

MMSC:空

彩信代理:空

彩信端口:空

彩信协议:WAP2.0

MCC:460

MNC:01

APN类型:default

MENU→保存

2、

名称:uniwap(3GWAP)

APN:UNIWAP(3GWAP)

代理:10.0.0.172

端口:80

用户名:空

密码:http://www.wo.com.cn

服务器:空

MMSC:http://mmsc.myuni.com.cn

彩信代理:010.000.000.172

彩信端口:80

彩信协议:WAP2.0

MCC:460

MNC:01

APN类型:mms

MENU→保存

3、启用3GNET(在APN设置里有3GNET与3GWAP两个启用选项)

中国电信

1、

名称:NET

APN:CTNET

代理:10.0.0.200

端口:80

用户名:空

密码:空

服务器:空

MMSC:空

彩信代理:空

彩信端口:空

彩信协议:WAP2.0

MCC:460

MNC:00

APN类型:default

MENU→保存

2、

APN:CTWAP

代理:10.0.0.200

端口:80

用户名:空

密码:空

服务器:空

MMSC:空

彩信代理:空

彩信端口:空

彩信协议:WAP2.0

MCC:460

MNC:00

APN类型:default

MENU→保存

3、

名称:MMSC

APN:CTWAP

代理:10.0.0.200

端口:80

用户名:空

密码:空

服务器:空

MMSC:10.0.0.200

彩信代理:10.0.0.200

彩信端口:80

彩信协议:WAP2.0

MCC:460

MNC:00

APN类型:mms

完成

4、启用CTNET

如果还是用不了,则可在拨号面板里输入*#*#4636#*#*进入手机信息→设置首选的网络类型,选择GSMONLY

.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: