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 spring sky * Email vipa1888@163.com * QQ: 840950105 my name: shi mingzheng * uses httpurlconnection to request the Internet * @ Param context object * @ Param requesturl request URL * @ Param request parameter * @ return returns an inputstream stream */public static inputstream gethttpurlconnectioninputstream (context, string requesturl, Map <string, string> param) {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 = new proxy (java.net. proxy. type. HTTP, new inetsocketaddress ("10.0.0.172", 80); Conn = (httpurlconnection) URL. openconnection (proxy);} else {conn = 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 ). appen D ("= "). 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 spring sky * Email vipa1888@163.com * QQ: 840950105 my name: shi mingzheng * uses httpurlconnection to request the Internet * @ Param context object * @ Param requesturl request URL * @ Param request parameter * @ return returns an inputstream stream */public static inputstream gethttpclientinputstream (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);} 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