Android uses httpurlconnection and httpclient to configure the Internet proxy gateway

Source: Internet
Author: User

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 ;}
 
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.