Android apps often interact with the server side, which requires a mobile client to send a network request, sorting through four common network requests.
The HttpURLConnection class in the java.net package
Get mode:
Get mode request public static void Requestbyget () throws Exception {String path = "https://reg.163.com/logins.jsp?id= Helloworld&pwd=android ";//Create a new URL object url url = new URL (path);//Open a httpurlconnection connection httpurlconnection urlconn = ( httpurlconnection) url.openconnection ();//Set Connection Timeout time Urlconn.setconnecttimeout (5 * 1000);//Start Connection urlconn.connect ();// Determine if the request was successful if (urlconn.getresponsecode () = = http_200) {//Get the returned data byte[] "the Readstream" (Urlconn.getinputstream ()); LOG.I (Tag_get, "GET method request succeeded, return data as follows:"); LOG.I (Tag_get, new String (data, "UTF-8"));} else {log.i (Tag_get, "GET method request Failed");} Close connection Urlconn.disconnect ();}
Post mode:
//Post mode request Public Static voidRequestbypost ()throwsthrowable {String path= "Https://reg.163.com/logins.jsp"; //the requested parameter is converted to a byte arrayString params = "id=" + urlencoder.encode ("HelloWorld", "UTF-8") + "&pwd=" + Urlencoder.encode ("Android", "UTF-8"); byte[] PostData =params.getbytes (); //Create a new URL objectURL url =NewURL (path); //Open a httpurlconnection connectionHttpURLConnection Urlconn =(HttpURLConnection) url.openconnection (); //Setting the connection time-out periodUrlconn.setconnecttimeout (5 * 1000); //The POST request must be set to allow outputUrlconn.setdooutput (true); //The POST request cannot use the cacheUrlconn.setusecaches (false); //Set as POST requestUrlconn.setrequestmethod ("POST"); Urlconn.setinstancefollowredirects (true); //Configuration Request Content-typeUrlconn.setrequestproperty ("Content-type", "Application/x-www-form-urlencode"); //Start ConnectionUrlconn.connect (); //Send Request ParametersDataOutputStream dos =NewDataOutputStream (Urlconn.getoutputstream ()); Dos.write (PostData); Dos.flush (); Dos.close (); //determine if the request was successful if(Urlconn.getresponsecode () = =http_200) { //get the returned data byte[] data =Readstream (Urlconn.getinputstream ()); LOG.I (Tag_post,"The post request was successful and the data returned is as follows:"); LOG.I (Tag_post,NewString (data, "UTF-8")); } Else{log.i (tag_post,"Post Mode request Failed"); }}
HttpGet and HttpPost classes in the Org.apache.http package
Get mode:
//HttpGet Way Request Public Static voidRequestbyhttpget ()throwsException {String path= "Https://reg.163.com/logins.jsp?id=helloworld&pwd=android"; //New HttpGet ObjectHttpGet HttpGet =Newhttpget (path); //Get HttpClient ObjectHttpClient HttpClient =Newdefaulthttpclient (); //Get HttpResponse InstanceHttpResponse Httpresp =Httpclient.execute (HttpGet); //judgment is enough to ask for success if(Httpresp.getstatusline (). Getstatuscode () = =http_200) { //get the returned dataString result = entityutils.tostring (Httpresp.getentity (), "UTF-8"); LOG.I (Tag_httpget,"HttpGet method request succeeded, return data as follows:"); LOG.I (tag_httpget, result); } Else{log.i (Tag_httpget,"HttpGet Mode request Failed"); }}
Post mode:
//HttpPost Way Request Public Static voidRequestbyhttppost ()throwsException {String path= "Https://reg.163.com/logins.jsp"; //New HttpPost ObjectHttpPost HttpPost =Newhttppost (path); //Post Parameterslist<namevaluepair> params =NewArraylist<namevaluepair>(); Params.add (NewBasicnamevaluepair ("id", "HelloWorld")); Params.add (NewBasicnamevaluepair ("pwd", "Android")); //Set character setshttpentity entity =Newurlencodedformentity (params, HTTP. UTF_8); //Set parameter Entitieshttppost.setentity (entity); //Get HttpClient ObjectHttpClient HttpClient =Newdefaulthttpclient (); //Get HttpResponse InstanceHttpResponse Httpresp =Httpclient.execute (HttpPost); //judgment is enough to ask for success if(Httpresp.getstatusline (). Getstatuscode () = =http_200) { //get the returned dataString result = entityutils.tostring (Httpresp.getentity (), "UTF-8"); LOG.I (Tag_httpget,"HttpPost method request succeeded, return data as follows:"); LOG.I (tag_httpget, result); } Else{log.i (Tag_httpget,"HttpPost Mode request Failed"); }}
Several ways to request Web requests in Android