For Android networking, httpurlconneciton and httpclient are mainly used for networking. When mobile phones are connected, WiFi is preferred, and mobile networks are selected. Here, mobile networks refer to cmwap.
We all know that the proxy address and port need to be set for cmwap connection, so, AndroidProgramHow to Set proxy?
1. httpurlconnection sets proxy
// When we are using China Mobile's mobile phone network, the following method can be obtained directly to obtain 10.0.0.172, port 80 string host = android.net. proxy. getdefaulthost (); // use andorid.net. proxy can obtain the default proxy address int Port = android.net. proxy. getdefaultport (); // use andorid.net. proxy can obtain the default proxy port socketaddress SA = new inetsocketaddress (host, Port); // defines the proxy. The proxy here is derived from Java. netproxy proxy = new proxy (java.net. proxy. type. HTTP, SA); Url geturl = new URL ("www.baidu.com"); httpurlconnection con = (httpurlconnection) geturl. openconnection (proxy); // sets proxy
2. httpclient set proxy
Defaulthttpclient httpclient = new defaulthttpclient (); string host = proxy. getdefaulthost (); // The proxy is derived from Android. netint Port = proxy. getport (context); // same as httphost = new httphost (host, Port); // sets the proxy httpclient. getparams (). setparameter (connrouteparams. default_proxy, httphost); httpget = new httppost ("<a href =" http://www.baidu.com "> www.baidu.com </a>"); httpresponse response=httpclient.exe cute (httpget );
3. Determine whether the mobile phone is connected to the Internet and whether it is WiFi
Boolean isconnect () {connectivitymanager CM = (connectivitymanager) This. getsystemservice (context. connectivity_service); If (CM! = NULL) {return true;} return false;} Boolean iswifi () {connectivitymanager CM = (connectivitymanager) Context. getsystemservice (context. connectivity_service); If (CM! = NULL) {networkinfo ni = cm. getactivenetworkinfo (); If (! Ni. gettypename (). equals ("WiFi") {/** Ni. the gettypenmae () value may be * WiFi, indicating WiFi networking * mobile, indicating that GPRS and EGPRS * 3G networks have not been tested * WiFi and (e) GPRS cannot coexist. If both are enabled, the system only supports WiFi */return true;} return false ;}