Import Java. Io. bufferedreader; Import Java. Io. inputstreamreader; Import Java.net. urlencoder; Import Java. Security. keystore; Import Java. util. iterator; Import Java. util. List; Import Java. util. Map; Import Java. util. Map. entry; Import Org. Apache. http. httpresponse; Import Org. Apache. http. httpversion; Import Org. Apache. http. Client. httpclient; Import Org. Apache. http. Client. entity. urlencodedformentity; Import Org. Apache. http. Client. Methods. httpget; Import Org. Apache. http. Client. Methods. httppost; Import Org. Apache. http. Conn. clientconnectionmanager; Import Org. Apache. http. Conn. scheme. plainsocketfactory; Import Org. Apache. http. Conn. scheme. scheme; Import Org. Apache. http. Conn. scheme. schemeregistry; Import Org. Apache. http. Conn. SSL. sslsocketfactory; Import Org. Apache. http. impl. Client. defaulthttpclient; Import Org. Apache. http. impl. Conn. tsccm. threadsafeclientconnmanager; Import Org. Apache. http. Message. basicnamevaluepair; Import Org. Apache. http. Params. basichttpparams; Import Org. Apache. http. Params. httpparams; Import Org. Apache. http. Params. httpprotocolparams; Import Org. Apache. http. Protocol. HTTP; Import Org. Apache. http. util. entityutils; Import Android. content. context; Import Android.net. connectivitymanager; /** * Network tool ** @ Author Malinkang * */ Public Class Netutils { /** * Determine the network connection status ** @ Return True, available; false, unavailable */ Public Static Boolean Isopennetwork (context) {connectivitymanager connmanager = (Connectivitymanager) Context. getsystemservice (context. connectivity_service ); If (Connmanager. getactivenetworkinfo ()! = Null ){ Return Connmanager. getactivenetworkinfo (). isavailable ();} Return False ;} /** * GET request ** @ Param Urlstring * @ Param Params * @ Return */ Public Static String getrequest (string urlstring, Map <string, string> Params ){ Try {Stringbuilder urlbuilder = New Stringbuilder (); urlbuilder. append (urlstring ); If ( Null ! = Params) {urlbuilder. append ( "? " ); Iterator <Entry <string, string> iterator = Params. entryset (). iterator (); While (Iterator. hasnext () {entry <String, string> param =Iterator. Next (); urlbuilder. append (urlencoder. encode (Param. getkey (), UTF-8" ). Append ( '=' ). Append (urlencoder. encode (Param. getvalue (), UTF-8" )); If (Iterator. hasnext () {urlbuilder. append ( '&' );}}} // Create an httpclient object Httpclient client =Getnewhttpclient (); // Create an httpget object by sending a GET request Httpget getmethod = New Httpget (urlbuilder. tostring (); httpresponse response = Client.exe cute (getmethod ); // Get status code Int Res = Response. getstatusline (). getstatuscode (); If (RES = 200 ) {Stringbuilder Builder =New Stringbuilder (); // Get Response content Bufferedreader reader = New Bufferedreader ( New Inputstreamreader (response. getentity (). getcontent ())); For (String S = reader. Readline (); s! = Null ; S = Reader. Readline () {builder. append (s );} Return Builder. tostring ();}} Catch (Exception e ){} Return Null ;} /** * POST request ** @ Param Urlstring * @ Param Params * @ Return */ Public Static String postrequest (string urlstring, list <Basicnamevaluepair> Params ){ Try { // 1. Create an httpclient object Httpclient client = Getnewhttpclient (); // 2. Send a GET request to create an httpget object Httppost postmethod = New Httppost (urlstring); postmethod. setentity ( New Urlencodedformentity (Params, HTTP. utf_8); httpresponse response = Client.exe cute (postmethod ); Int Statuecode = Response. getstatusline (). getstatuscode (); If (Statuecode = 200 ) {System. Out. println (statuecode ); Return Entityutils. tostring (response. getentity ());}} Catch (Exception e ){} Return Null ;} // When saving + the current number of seconds, Public Static Long Expires (string second) {long l = Long. valueof (second ); Return L * 1000l + System. currenttimemillis ();} Private Static Httpclient getnewhttpclient (){ Try {Keystore truststore = Keystore. getinstance (keystore. getdefaulttype (); truststore. Load ( Null , Null ); Sslsocketfactory SF = New Sslsocketfactoryex (truststore); SF. sethostnameverifier (sslsocketfactory. allow_all_hostname_verifier); httpparams Params = New Basichttpparams (); httpprotocolparams. setversion (Params, httpversion. http_1_1); httpprotocolparams. setcontentcharset (Params, HTTP. utf_8); schemeregistry Registry = New Schemeregistry (); Registry. Register ( New Scheme ("HTTP" , Plainsocketfactory. getsocketfactory (), 80 ); Registry. Register ( New Scheme ("HTTPS", SF, 443 ); Clientconnectionmanager CCM = New Threadsafeclientconnmanager (Params, registry ); Return New Defaulthttpclient (CCM, Params );} Catch (Exception e ){ Return New Defaulthttpclient ();}}}
Another encapsulated GET request is frequently used recently:
Public Class Httputils { Private Final Static String tag = "easytokensevice" ; Private Final Static Int Connectiontimeout = 5000 ; Private Static Inputstream = Null ; Private Static String urlstr = Null ; Private Static Boolean Isconnecting; /** * Encapsulate http get requests ** @ Param URL * @ Return Is */ Public Static Inputstream get (string URL) Throws Ioexception, exception {urlstr = URL; isconnecting = True ; Httpget = New Httpget (urlstr); httpparams httpparameters = New Basichttpparams (); httpconnectionparams. setconnectiontimeout (httpparameters, connectiontimeout); defaulthttpclient httpclient = New Defaulthttpclient (httpparameters); httpresponse response =Httpclient.exe cute (httpget ); If (Response. getstatusline (). getstatuscode () = Httpstatus. SC _ OK) {httpentity entity = Response. getentity (); inputstream = Entity. getcontent (); Return Inputstream ;} Else Return Null ;}}