Android adds a China Mobile proxy for HttpClient and HttpURLConnection

Source: Internet
Author: User

 

In android, you usually need to make a network judgment before connecting to the Internet to determine the current network status! Then start requesting the network

When we use the wap network, we must use the China Mobile proxy in the program! In this way, the mobile phone can access the internet normally!

In android, there are two request methods: HttpURLConnection and HttpClient. If the network status is wap, you must add a China Mobile proxy for both requests!

Method 1: HttpURLConnection

 

 

/**

* @ Author sky

* Email vipa1888@163.com

* QQ: 840950105

* Use HttpURLConnection to request the Internet

* @ Param context object

* @ Param requestUrl the request URL

* @ Param Request Parameters

* @ Return returns an inputstream stream.

*/

Public static InputStream getHttpURLConnectionInputStream (Context context, String requestUrl, Map <String, String> param ){

URL url;

HttpURLConnection conn = null;

InputStream input = null;

Try {

Url = new URL (requestUrl );

If (getAPNType (context) = NetWorkUtil. CMWAP) // when the requested network is wap, you need to add the China Mobile proxy

{

Proxy proxy = new Proxy (java.net. Proxy. Type. HTTP, new InetSocketAddress ("10.0.0.172", 80 ));

Conn = (HttpURLConnection) url. openConnection (proxy );

}

Conn = (HttpURLConnection) url. openConnection ();

Conn. setConnectTimeout (10000); // request timeout

Conn. setRequestMethod ("POST"); // Request Method

Conn. setReadTimeout (1000); // read timeout

Conn. setDoOutput (true );

Conn. setDoInput (true );

Conn. setUseCaches (false );

Conn. setRequestProperty ("Charset", "UTF-8 ");

Conn. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded ");

OutputStream OS = conn. getOutputStream ();

StringBuilder sb = new StringBuilder ();

Iterator <String> it = param. keySet (). iterator ();

While (it. hasNext ()){

String key = it. next ();

String value = param. get (key );

Sb. append (key). append ("="). append (value). append ("&");

}

String p = sb. toString (). substring (0, sb. length ()-1 );

System. out. println ("request parameter" + p );

OS. write (p. getBytes ("UTF-8 "));

OS. close ();

If (conn! = Null)

{

Input = conn. getInputStream ();

}

} Catch (Exception e ){

E. printStackTrace ();

}

Return input;

}

 

 

 

The above method is HttpURLConnection, which is also commonly used in android development. I hope my friends will be familiar with it!

Method 2: HttpClient

 

/**

* @ Author sky

* Email vipa1888@163.com

* QQ: 840950105

* Use HttpURLConnection to request the Internet

* @ Param context object

* @ Param requestUrl the request URL

* @ Param Request Parameters

* @ Return returns an inputstream stream.

*/

Public static InputStream getHttpClientInputStream (Context context, String requestUrl, Map <String, String> param) throws Exception {

HttpClient client = new DefaultHttpClient ();

If (getAPNType (context) = NetWorkUtil. CMWAP) // when the requested network is wap, you need to add the China Mobile proxy

{

HttpHost proxy = new HttpHost ("10.0.0.172", 80 );

Client. getParams (). setParameter (ConnRoutePNames. DEFAULT_PROXY,

Proxy );

} Www.2cto.com

HttpPost hp = new HttpPost (requestUrl );

Hp. setHeader ("Charset", "UTF-8 ");

Hp. setHeader ("Content-Type", "application/x-www-form-urlencoded ");

List <BasicNameValuePair> list = new ArrayList <BasicNameValuePair> ();

Iterator <String> it = param. keySet (). iterator ();

While (it. hasNext ()){

String key = it. next ();

List. add (new BasicNameValuePair (key, param. get (key )));

}

Hp. setEntity (new UrlEncodedFormEntity (list, "UTF-8 "));

HttpResponse response = null;

Response = client.exe cute (hp );

Return response. getEntity (). getContent ();

}

 

This httpClient implements the android built-in DefaultHttpClient, so it is very convenient to use!

However, I found that HttpClient is better than HttpURLConnection, because HttpURLConnection has many problems when using wap for Internet requests (I have a deep understanding (for example, the request has no response, poor signals may cause unknown errors)

Now that you are familiar with the two request methods, you can develop android online applications! Haha

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.