Then, "Android uses get to submit data to the server," this article to implement the Post method to submit data to the server
First, compare the Get mode and post mode:
To modify a layout:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " > <EditTextAndroid:id= "@+id/et_name"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter user name"Android:inputtype= "text" /> <EditTextAndroid:id= "@+id/et_pwd"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter password"Android:inputtype= "Textpassword" /> <ButtonAndroid:onclick= "Loginbyget"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Get mode login" /> <ButtonAndroid:onclick= "Loginbypost"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Post Mode login" /></LinearLayout>
Add code:
Packagecom.wuyudong.loginclient;ImportJava.io.ByteArrayOutputStream;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportAndroid.os.Build;ImportAndroid.os.Bundle;ImportAndroid.os.StrictMode;ImportAndroid.annotation.SuppressLint;ImportAndroid.annotation.TargetApi;Importandroid.app.Activity;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_name; PrivateEditText et_pwd; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_name=(EditText) Findviewbyid (r.id.et_name); Et_pwd=(EditText) Findviewbyid (R.ID.ET_PWD); Strictmode.threadpolicy Policy=NewStrictMode.ThreadPolicy.Builder (). Permitall (). build (); Strictmode.setthreadpolicy (Policy); } Public voidloginbyget (view view) {String name=Et_name.gettext (). toString (). Trim (); String pwd=Et_pwd.gettext (). toString (). Trim (); if(Textutils.isempty (name) | |Textutils.isempty (PWD)) {Toast.maketext ( This, "User name password cannot be empty", 0). Show (); } Else { //impersonate an HTTP request, submit data to the serverString Path = "Http://169.254.168.71:8080/web/LoginServlet?username=" + name + "&password=" +pwd; Try{URL URL=NewURL (path); //2. Establish an HTTP connectionHttpURLConnection conn =(httpurlconnection) URL. OpenConnection (); //3. Set up some request methodsConn.setrequestmethod ("GET");//Note get word captions must be capitalizedConn.setrequestproperty ("User-agent", "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 "); intCode = Conn.getresponsecode ();//the server's response code is OK//404 Page Not Found// //503 Server Internal Error if(Code = = 200) {InputStream is=Conn.getinputstream (); //Convert the content of is to a stringBytearrayoutputstream BOS =NewBytearrayoutputstream (); byte[] buffer =New byte[1024]; intLen =-1; while(len = is.read (buffer))! =-1) {bos.write (buffer,0, Len); The String result=NewString (Bos.tobytearray ()); Is.close (); Toast.maketext ( This, result, 0). Show (); } Else{Toast.maketext ( This, "request failed, reason for failure:" + code, 0). Show (); } } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "The request failed, please check the Logcat log console", 0). Show (); } } } /*** Submit data to Server using Post * *@paramView*/ Public voidloginbypost (view view) {String name=Et_name.gettext (). toString (). Trim (); String pwd=Et_pwd.gettext (). toString (). Trim (); if(Textutils.isempty (name) | |Textutils.isempty (PWD)) {Toast.maketext ( This, "User name password cannot be empty", 0). Show (); } Else { Try{String path= "Http://169.254.168.71:8080/web/LoginServlet?username=" + name + "&password=" +pwd; //1. Defining the request URLURL url =NewURL (path); //2. Establish an HTTP connectionHttpURLConnection conn =(httpurlconnection) URL. OpenConnection (); //3. Set parameters for some requestsConn.setrequestmethod ("POST"); Conn.setrequestproperty ("User-agent", "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 "); Conn.setrequestproperty ("Content-type", "Application/x-www-form-urlencoded"); String Data= "Username=" + name + "&password=" +pwd; Conn.setrequestproperty ("Content-length", data.length () + ""); Conn.setconnecttimeout (5000);//Setting the connection time-out periodConn.setreadtimeout (5000);//set the Read timeout time//4. Be sure to remember to set the data stream to the serverConn.setdooutput (true);//set up to write data to the serverConn.getoutputstream (). Write (Data.getbytes ()); intCode = Conn.getresponsecode ();//the server's response code is OK//404 Page Not Found// //503 Server Internal Error if(Code = = 200) {InputStream is=Conn.getinputstream (); //Convert the content of is to a stringBytearrayoutputstream BOS =NewBytearrayoutputstream (); byte[] buffer =New byte[1024]; intLen =-1; while(len = is.read (buffer))! =-1) {bos.write (buffer,0, Len); The String result=NewString (Bos.tobytearray ()); Is.close (); Toast.maketext ( This, result, 0). Show (); } Else{Toast.maketext ( This, "request failed, reason for failure:" + code, 0). Show (); } } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "The request failed, please check the Logcat log console", 0). Show (); } } }}
Android uses post to submit data to the server