cmwapアクセスポイントを使用してインターネットとそのソリューションにアクセスするAndroid



Android Using Cmwap Access Point Access Internet



/ / Check if the network is normal
private boolean checkNet(){

ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE)

netWrokInfo = manager.getActiveNetworkInfo()
if (netWrokInfo == null || !netWrokInfo.isAvailable()) {
Toast.makeText(this, 'The current network is not available, please open network', Toast.LENGTH_LONG).show()
return false
}
else if(netWrokInfo.getTypeName().equals('MOBILE')& netWrokInfo.getExt raInfo().equals('cmwap')){
Toast.makeText(this, 'cmwap network is not available, please select cmnet network', Toast.LENGTH_LONG).show()
return false
}else{

return true
}
}

/ **
* AndroidはcmwapGPRSを使用してネットワークを構築します
* /
CMWAPとCMNETは、チャイナモバイルが分割した2つのGPRSアクセスモードにすぎません。チャイナモバイルは、主にCMWAPにアクセスするときにGPRSネットワークのIP(10。*。*。*)にのみアクセスでき、ルーティングを介してインターネットにアクセスできないため、CMWAPに特定の制限を課しています。 CMWAPを使用して、インターネット上のWebページを閲覧します。これは、WAPゲートウェイプロトコルまたはそれが提供するHTTPプロキシサービスを介して実装されます。したがって、次の2つの条件を満たすアプリケーションのみが、チャイナモバイルのCMWAPアクセスモードで正常に動作します。
1.アプリケーションのネットワーク要求はHTTPプロトコルに基づいています。
2.アプリケーションサポートHTTPプロキシプロトコルまたはWAPゲートウェイプロトコル。
そのため、G1はCMWAPで正しく機能しません。
結論:CMWAPはモバイルに制限されており、理論的にはWAPネットワーク上でのみ使用され、CMNETはGPRSを使用してWWWを参照できます。
方法1:
URL url = new URL('http://10.0.0.172/img/baidu_logo.gif')
HttpURLConnection conn = (HttpURLConnection) url.openConnection()
conn.setRequestProperty('X-Online-Host', 'www.baidu.com')
conn.setDoInput(true)
conn.connect()
InputStream is = conn.getInputStream()
bitmap = BitmapFactory.decodeStream(is)
is.close()
conn.disconnect()

方法2:
コード:
package org.apache.http.examp les.client

import org.apache.http.Header
import org.apache.http.HttpEntity
import org.apache.http.HttpHost
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.conn.params.ConnRoutePNames
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.util.EntityUtils

public class ClientExecuteProxy {

public static void main(String [] args)throws Exception {

HttpHost proxy = new HttpHost( '10.0.0.172', 80, 'http')
HttpHost target = new HttpHost('YOUR_TARGET_IP', 80, 'http')

DefaultHttpClient httpclient = new DefaultHttpClient()
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy)


HttpGet req = new HttpGet('/')

System.out.println('executing request to ' + target + ' via ' + proxy)
HttpResponse rsp = httpclient.execute(target, req)
HttpEntity entity = rsp.getEntity()

System.out.println('----------------------------------------')
System.out.println(rsp.getStatusLine())
Header[] headers = rsp.getAllHeaders()
for (int i = 0 i System.out.println(headers)
}
System.out.println('----------------------------------------')

if (entity != null) {
System.out.println(EntityUtils.toString(entity))
}

// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown()
}

}


[size = x-large] AndroidでGPRS接続を作成します[/ size]

private boolean openDataConnection() {
// Set up data connection.
DataConnection conn = DataConnection.getInstance()

if (connectMode == 0) {
ret = conn.openConnection(mContext, 'cmwap', 'cmwap', 'cmwap')
} else {
ret = conn.openConnection(mContext, 'cmnet', '', '')
}

}


[url = 'http://www.cnblogs.com/-OYK/archive/2012/03/17/2403241.html'] AndroidでのWAPとNETの実装[/ url]

import java.net.HttpURLConnection
import java.net.InetSocketAddress
import java.net.URL
/**
* Downloader
*/
public class DownloadUtil {
private static final String TAG = 'Downloader'
/**
* @return InputStream download
*/
public static HttpURLConnection download(String url) {
HttpURLConnection conn = null
try {
String proxyHost = android.net.Proxy.getDefaultHost()
If (proxyHost != null) {//If it is wap mode, add gateway
java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(
android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort()))
conn = (HttpURLConnection) new URL(url).openConnection(p)
} else {
conn = (HttpURLConnection) new URL(url).openConnection()
}
// conn.setReadTimeout(5000)
conn.setConnectTimeout(10000)
conn.setRequestMethod('GET')
conn.setRequestProperty(
'Accept',
'image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*')
conn.setRequestProperty('Accept-Language', 'zh-CN')
conn.setRequestProperty('Referer', url)
conn.setRequestProperty('Charset', 'UTF-8')
conn.setRequestProperty(
'User-Agent',
'Mozilla/4.0 (compatible MSIE 8.0 Windows NT 5.2 Trident/4.0 .NET CLR 1.1.4322 .NET CLR 2.0.50727 .NET CLR 3.0.04506.30 .NET CLR 3.0.4506.2152 .NET CLR 3.5.30729)')
conn.setRequestProperty('Connection', 'Keep-Alive')
conn.connect()
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return conn
}
} catch (Exception e) {
e.printStackTrace()
}
return null
}
}


[size = x-large] Androidがネットワークステータスを判断します[/ size]
Androidを使用してネットワークに接続する場合、ネットワークに接続できるたびにではなく、この時点で、プログラムの開始時にネットワークのステータスを判断し、ネットワークがない場合は設定するようにユーザーにすぐに通知する必要があります。
ネットワークステータスを確認するには、最初に適切なアクセス許可が必要です。アクセス許可コードは次のとおりです。
つまり、ネットワークステータスへのアクセスが許可されます。

判定コードは次のとおりです。

private boolean NetWorkStatus() {

boolean netSataus = false
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)

cwjManager.getActiveNetworkInfo()

if (cwjManager.getActiveNetworkInfo() != null) {
netSataus = cwjManager.getActiveNetworkInfo().isAvailable()
}

if (netSataus) {
Builder b = new AlertDialog.Builder(this).setTitle('No network available')
.setMessage('Do you want to set up the network?')
b.setPositiveButton('Yes', new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent mIntent = new Intent('/')
ComponentName comp = new ComponentName(
'com.android.settings',
'com.android.settings.WirelessSettings')
mIntent.setComponent(comp)
mIntent.setAction('android.intent.action.VIEW')
startActivityForResult(mIntent,0) // If you need to do it again after the setting is completed, you can rewrite the operation code, no longer rewrite here.
}
}).setNeutralButton('No', new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel()
}
}).show()
}

return netSataus
}
/ / Through the above code to complete the judgment of the network status! Specifically how to choose the network to set up, have not figured out, and so on, and then write!