Android Learning--http Request

Source: Internet
Author: User
Tags response code

Package Cn.xm.hostrequest.biz;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.URL; Import Java.net.urlencoder;import java.util.arraylist;import Java.util.list;import Org.apache.http.header;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;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.impl.client.defaulthttpclient;import Org.apache.http.message.basicnamevaluepair;import Android.content.context;import Android.widget.Toast;import Cn.xm.hostrequest.tools.streamtools;import Com.loopj.android.http.asynchttpclient;import Com.loopj.android.http.asynchttpresponsehandler;import Com.loopj.android.http.requestparams;public Class Loginservice {public static Boolean Loginbyget (string path, string username,string password) {try {URL url = new URL (path + "? user=" + Urlencoder.encode (USErname, "utf-8") + "&pwd=" + urlencoder.encode (password, "Utf-8")); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (n); int code = Conn.getresponsecode (); if (code = =) {String response = Streamtools.readinputinputstream (Conn.getinputstream ()); if (response! = null && response.equals ("true")) { return true;}}} catch (Exception e) {e.printstacktrace ();} return false;} public static Boolean Loginbypost (string path, string username,string password) {try {//create Urlurl url = new URL (path);// Use the URL to open the connection httpurlconnection conn = (httpurlconnection) url.openconnection ();//Set request parameter Conn.setrequestmethod ("POST"); Post request Conn.setconnecttimeout (5000); Connection Timeout Time conn.setdooutput (true); To write out the output stream to the server//assemble a POST request data string "user=" + urlencoder.encode (username, "utf-8") + "&pwd=" + urlencoder.encod E (password, "Utf-8");//Set message header Conn.addrequestproperty ("Content-type", "application/x-www-form-urlencoded");// Set data length COnn.addrequestproperty ("Content-length", data.length () + "");//Take output stream and write data outputstream OS = Conn.getoutputstream (); O S.write (Data.getbytes ());//Executes the request and takes the response code int code = Conn.getresponsecode (); if (code = =) {String response = Streamtools.re Adinputinputstream (Conn.getinputstream ()); if (response! = null && response.equals ("true")) {return true;}}} catch (Exception e) {e.printstacktrace ();} return false;} public static Boolean Loginbyclientpost (string path, string username,string password) {try {//open httpClient = = Open Browser Httpc Lient client = new Defaulthttpclient ();//Set the URL to access = = Actionhttppost post in form = new HttpPost (path);//create post data = = data filled in form list<namevaluepair> parameters = new Arraylist<namevaluepair> (0);p Arameters.add (new Basicnamevaluepair ("User", username));p arameters.add (New Basicnamevaluepair ("pwd", password));// Set Data post.setentity (new urlencodedformentity (Parameters, "Utf-8"));//Send request and get response httpresponse response = Client.execute (post);//Get Response code int code= Response.getstatusline (). Getstatuscode (); if (code = =) {String Text = Streamtools.readinputinputstream ( Response.getentity (). getcontent ()); if (text! = null && text.equals ("true")) {return true;}}} catch (Exception e) {e.printstacktrace ();} return false;} public static Boolean Loginbyclientget (string path, string username,string password) {try {//Open httpclient== Open the browser httpclient client = new Defaulthttpclient ();//Generate the URL to access = = Input address HttpGet httpget = new HttpGet (path);//Send request = = Hit Enter h Ttpresponse response = Client.execute (httpget);//Take the status code int code = Response.getstatusline (). Getstatuscode (); if (Code = = () {String text = Streamtools.readinputinputstream (Response.getentity (). getcontent ()); if (text! = null && Text.equals ("true")) {return true;}}} catch (Exception e) {e.printstacktrace ();} return false;} public static void Loginbyasyncget (final context context, String path,string Username, string password) {asynchttpclient c lient = new Asynchttpclient (); Client.get (path + "? user="+ username +" &pwd= "+ password,new Asynchttpresponsehandler () {@Overridepublic void onsuccess (int statusCode, Heade R[] headers,byte[] responsebody) {string text = new String (responsebody), if ("true". Equals (text)) {Toast.maketext ( Context, "Landing success", 0). Show (); else {toast.maketext (context, "Login failed", 0). Show ();}} @Overridepublic void onfailure (int statusCode, header[] headers,byte[] responsebody, throwable error) {Toast.maketext ( Context, "Login failed", 0). Show ();}});  public static void Loginbyasyncpost (final context context, String path,string Username, string password) {Asynchttpclient Client = new Asynchttpclient (); Requestparams parm = new Requestparams ();p arm.put ("User", username);p arm.put ("pwd", password); client.post (path, Parm, New Asynchttpresponsehandler () {@Overridepublic void onsuccess (int statusCode, header[] headers,byte[] responsebody) { String text = new String (responsebody), if ("true". Equals (text)) {Toast.maketext (context, "login succeeded", 0). Show ();} else {Toast . Maketext (Context, "Login failed", 0). Show ();}} @Overridepublic void onfailure (int statusCode, header[] headers,byte[] responsebody, throwable error) {Toast.maketext ( Context, "Login failed", 0). Show ();}});}}

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.