Implement WAP-based networking in Android code

Source: Internet
Author: User

Both China Mobile, China Unicom, and China Telecom provide at least two types of APN: WAP and net. Among them, the net method is the same as the WiFi method, without any settings, You can freely access all types of websites, and the WAP method requires the mobile phone to first set proxy server and port number information, you can only access websites of the HTTP protocol type.

1) The Mobile WAP name is cmwap and the net name is cmnet;

2) China Unicom's WAP name is uniwap, net name is uninet, China Unicom's 3g wap name is 3 gwap, and net name is 3 GNET;

3) China Telecom's WAP name is ctwap and net name is ctnet;

The WAP proxy servers of China Mobile and China Unicom are both 10.0.0.172 and the port number is 80. The WAP proxy server of China Telecom is 10.0.0.200 and the port number is 80.

In the Android system, the API of the APN network is hidden. Therefore, to obtain the APN settings of the mobile phone, you must use contentprovider to query the database. The URI address to query is:

Get all APN lists: Content: // telephony/carriers;

Get the currently set APN: Content: // telephony/carriers/preferapn;

Get the APN: Content: // telephony/carriers/current with current = 1;

The following code gets the preferred APN settings and inherits httpclient to implement our own proxy httpclient class. First, let's take a look at the implementation of the management class of APN. The main function of this class is to obtain the proxy server and port number of APN. The URI used for query is as follows:

This URI uses contentresolver to obtain the cursor object, and then queries the operation, investigating the APN, proxy, and port set by the current mobile phone respectively. If the mobile phone's proxy is not set, you need to determine the current proxy server address and port number based on the APN. The detailed code is as follows:

After obtaining the proxy and port set by the WAP of the current mobile phone through the apnmanager class, we can construct our own proxy httpclient. This class is defined as proxyhttpclient. In the constructor of this class, first obtain the apnmanager instance, then obtain the proxy server proxy and port value, construct the httphost instance through these two parameters, and set the host instance to connrouteparams. the detailed code for default_proxy is as follows:

The apnmanager class is defined as follows:

Package COM. HUST. iprai; import android. content. contentresolver; import android. content. context; import android. database. cursor; import android.net. connectivitymanager; import android.net. networkinfo; import android.net. uri; public class apnmanager {public static final URI preferred_apn_uri; private string mapn; // Access Point name private string mport; // port number private string mproxy; // Proxy Server private Boolean musewap; // Whether WAP static {preferred_apn_uri = URI is being used. parse ("content: // telephony/carriers/preferapn"); // get the currently set APN} public apnmanager (context) {checknetworktype (context );} /*** get the currently set APN parameters * @ Param context */private void checkapn (context) {contentresolver = context. getcontentresolver (); Uri uri = preferred_apn_uri; string [] apninfo = new string [3]; apninfo [0] = "APN"; apninfo [1] = "proxy"; apninfo [2] = "Port"; cursor = contentresolver. query (Uri, apninfo, null); If (cursor! = NULL) {While (cursor. movetofirst () {This. mapn = cursor. getstring (cursor. getcolumnindex ("APN"); this. mproxy = cursor. getstring (cursor. getcolumnindex ("proxy"); this. mport = cursor. getstring (cursor. getcolumnindex ("Port"); // The proxy is empty if (this. mproxy = NULL) | (this. mproxy. length () <= 0) {string APN = This. mapn. touppercase (); // China Mobile WAP settings: APN: cmwap; Proxy: 10.0.0.172; port: 80 // China Unicom WAP settings: APN: uniwap; Generation Reason: 10.0.0.172; port: 80 // China Unicom WAP settings (3G): APN: 3 gwap; Proxy: 10.0.0.172; port: 80 If (APN. equals ("cmwap") | (APN. equals ("uniwap") | (APN. equals ("3 gwap") {This. musewap = true; this. mproxy = "10.0.0.172"; this. mport = "80"; break;} // China Telecom WAP settings: APN (or access point name): ctwap; Proxy: 10.0.0.200; port: 80 If (APN. equals ("ctwap") {This. musewap = true; this. mproxy = "10.0.0.200"; this. mport = "80"; break;} This. mport = "80 "; This. musewap = true; break;} This. musewap = false; cursor. close ();}/*** check whether the current network type is WIFI or WAP * @ Param context */private void checknetworktype (context) {networkinfo = (connectivitymanager) context. getapplicationcontext (). getsystemservice (context. connectivity_service )). getactivenetworkinfo (); If (networkinfo! = NULL) {If (! "WiFi ". equals (networkinfo. gettypename (). tolowercase () {checkapn (context); return;} This. musewap = false ;}}/*** determine the current network connection status * @ Param context * @ return */public static Boolean isnetworkconnected (context) {networkinfo = (connectivitymanager) context. getapplicationcontext (). getsystemservice ("connectivity ")). getactivenetworkinfo (); If (networkinfo! = NULL) {return networkinfo. isconnectedorconnecting ();} return false;} Public String getapn () {return this. mapn;} Public String getproxy () {return this. mproxy;} Public String getproxyport () {return this. mport;} public Boolean iswapnetwork () {return this. musewap ;}}


The complete definition of the proxyhttpclient class is as follows:

Package COM. HUST. iprai; import android. content. context; import android. text. textutils; import android. util. log; import Org. apache. HTTP. httphost; import Org. apache. HTTP. conn. params. connrouteparams; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. params. httpconnectionparams; import Org. apache. HTTP. params. httpparams; import Org. apache. HTTP. params. httpprotocolparams; public class pro Xyhttpclient extends defaulthttpclient {Private Static final int http_timeout_ms = 30*1000; Private Static final int buffer_size = 1024*8; Private Static final string tag = proxyhttpclient. class. getsimplename (); Private runtimeexception mleakedexception = new illegalstateexception ("proxyhttpclient created and never closed"); Private string mport; private string mproxy; private Boolean musewap; Public proxyhttpclient (context) {This (context, null, null);} public proxyhttpclient (context, apnmanager Manager) {This (context, null, manager );} public proxyhttpclient (context, string useragent) {This (context, useragent, null);} public proxyhttpclient (context, string useragent, apnmanager manager) {If (Manager = NULL) {manager = new apnmanager (context );} This. musewap = manager. iswapnetwork (); this. mproxy = manager. getproxy (); this. mport = manager. getproxyport (); If (this. musewap) {httphost host = new httphost (this. mproxy, integer. valueof (this. mport ). intvalue (); getparams (). setparameter (connrouteparams. default_proxy, host); // sets proxy} httpconnectionparams. setconnectiontimeout (getparams (), http_timeout_ms); httpconnectionparams. setsotimeout (getpar AMS (), http_timeout_ms); httpconnectionparams. setsocketbuffersize (getparams (), buffer_size); If (! Textutils. isempty (useragent) {httpprotocolparams. setuseragent (getparams (), useragent) ;}} public void close () {If (this. mleakedexception! = NULL) {getconnectionmanager (). shutdown (); this. mleakedexception = NULL ;}} protected httpparams createhttpparams () {httpparams Params = super. createhttpparams (); httpprotocolparams. setuseexpectcontinue (Params, false); Return Params;} protected void finalize () throws throwable {super. finalize (); If (this. mleakedexception! = NULL) {log. E (TAG, "leak found", this. mleakedexception );}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.