Android emulator accesses web service through proxy

Source: Internet
Author: User

1. Start the simulator (Android 2.2 emulator) and go to Settings> wireless & Network> mobile networks> Access Point names.

Then open telkila.

 

2. Set the following as follows:
-Proxy: your proxy address
-Port: your proxy Port

After the above two steps, the system's browser can access the Internet. HoweverProgramWebview or httpconnection in does not support Internet access. What should we do?

 

First, to allow the program to access the Internet, add permission and the second permission to add a proxy:

<Uses-Permission Android: Name = "android. Permission. Internet"/>
<Uses-Permission Android: Name = "android. Permission. access_network_state"/>

1. For webview

Mwebview = (webview) findviewbyid (R. Id. webview1 );
Mwebview. getsettings ()
. Setcachemode (websettings. load_cache_else_network );
Mwebview. enableplatformconfigurications ();

 

Done!

 

2. For urlconnection:

URL url = new URL (urlstr);
string host = android.net. proxy. getdefaulthost ();
int Port = android.net. proxy. getdefaultport ();
socketaddress SA = new inetsocketaddress (host, Port);

Proxy proxy = new proxy (java.net. Proxy. type. HTTP,
SA );

Urlconnection conn = URL. openconnection (proxy );
Inputstream is = conn. getinputstream ();

 

Done!

 

3.Httpclient:

Defaulthttpclient httpclient =NewDefaulthttpclient ();

String host = proxy. getdefaulthost ();// Here the proxy is from android.net

IntPort = proxy. getport (context );// Same as above

Httphost =NewHttphost (host, Port );

// Set proxy

Httpclient. getparams (). setparameter (connrouteparams. default_proxy, httphost );

Httpget =NewHttppost ("<A href ="HTTP:// Www.baidu.com "> www.baidu.com </a> ");

Httpresponse response=httpclient.exe cute (httpget );

 

4. The following content is reproduced:

Determine whether a mobile phone is connected to IOT Platform

Boolean isconnect (){
Connectivitymanager CM = (connectivitymanager) This. getsystemservice (context. connectivity_service );
If (CM! = NULL ){
Return true;
}
Return false;
}

 

Determine whether the current network is WiFi

Boolean iswifi (){
Connectivitymanager CM = (connectivitymanager) Context. getsystemservice (context. connectivity_service );
If (CM! = NULL ){
Networkinfo ni = cm. getactivenetworkinfo ();
If (! Ni. gettypename (). Equals ("WiFi ")){
/*
* NI. gettypenmae () may take the following values:
* WiFi, indicating WiFi networking
* Mobile, indicating 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;
}

 

By comprehensively determining the network type, we can determine whether to set up a proxy to achieve correct networking.

Example 1

Httpurlconnection con = NULL;
URL posturl = new URL ("www.baidu.com ");
Boolean isproxy = false;
// Network detection
Connectivitymanager CM = (connectivitymanager) Context. getsystemservice (context. connectivity_service );
Boolean isproxy = false;
If (CM! = NULL ){
Networkinfo ni = cm. getactivenetworkinfo ();
If (Ni! = NULL ){
If (! Ni. gettypename (). Equals ("WiFi ")){
Isproxy = true;
}
}
}
If (isproxy ){
Proxy proxy = new proxy (java.net. Proxy. type. HTTP, new inetsocketaddress (android.net. Proxy. getdefaulthost (), android.net. Proxy. getdefaultport ()));
Con = (httpurlconnection) posturl. openconnection (proxy );
} Else {
Con = (httpurlconnection) posturl. openconnection ();
}

 

Example 2

Defaulthttpclient httpclient = new defaulthttpclient ();
// Network detection
Connectivitymanager CM = (connectivitymanager) Context. getsystemservice (context. connectivity_service );
If (CM! = NULL ){
Networkinfo ni = cm. getactivenetworkinfo ();
If (Ni! = NULL ){
If (! Ni. gettypename (). Equals ("WiFi ")){
// Set proxy
String host = proxy. getdefaulthost ();
Int Port = proxy. getport (context );
Httphost = new httphost (host, Port );
Httpclient. getparams (). setparameter (connrouteparams. default_proxy, httphost );
}
}
}

 

http://blog.csdn.net/fhy_2008/article/details/7027892

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.