The Get method is said earlier. Now try the post
#1. First use a new servlet to process the login request
Package Com.wzw.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * Servlet Implementation class Loginservlet */public class LoginServlet Extends HttpServlet {private static final long serialversionuid = 1L; /** * @see httpservlet#httpservlet () */public Loginservlet () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {String username=request.getparameter ("username"); String password=request.getparameter ("password"); if (Username.equals ("admin") &&password.equals ("123456") {Response.getwriter (). println ("Success");} Else{response.getwriter (). println ("Failed");}} /** * @see Httpservlet#dopost(HttpServletRequest request, httpservletresponse response) */protected void DoPost (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated method stub
<span style= "White-space:pre" ></span>this.doget (Request,response);}}
#2. Layout files are also simple
<linearlayout xmlns: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:orien tation= "Vertical" tools:context= ". Mainactivity "> <edittext android:layout_margintop=" 10dp "android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:id= "@+id/et_username" android:hint= "Please enter first name"/> <EditTex T android:layout_margintop= "10DP" android:layout_width= "fill_parent" android:layout_height= "Wrap_cont Ent "android:id=" @+id/et_password "android:hint=" Please enter the password "/> <button android:layout_height=" Wrap_c Ontent "android:layout_width=" wrap_content "android:onclick=" Doget "android:text=" Get Way Commit "/> <Button and roid:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:onclick= "DoPost" android:text= "PO St Way Submit "/></linearlayout>
#3在mainactivity中实现方法
Package Com.wzw.submitdata;import Com.wzw.submitdata.utils.netutil;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.widget.EditText;import Android.widget.toast;public class Mainactivity extends Activity {private EditText etusername;private EditText Etpassword; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); etusername = (EditText) Findviewbyid (r.id.et_username); Etpassword = ( EditText) Findviewbyid (R.id.et_password);} public void doget (View v) {final string Username=etusername.gettext (). toString (); final String password= Etpassword.gettext (). toString (); New Thread (New Runnable () {@Overridepublic void Run () {//access network to be implemented in a child thread, using get to take data final String state=netutil.loginofget (username, password);//execution on the main thread runonuithread (new Runnable () {public void run () {// is to operate on the main thread, with the result Toast.maketext (mainactivity.this, State, 0). Show ();});}). Start ();} public void DOpost (View v) {final string Username=etusername.gettext (). toString (); final string Password=etpassword.gettext (). ToString (); New Thread (New Runnable () {@Overridepublic void Run () {final String state=netutil.loginofpost (username, Password)///execute on the main thread runonuithread (new Runnable () {public void run () {///is on the main thread, the result Toast.maketext ( Mainactivity.this, State, 0). Show ();});}). Start ();}}
#4. Methods for handling get and post specifically
Package Com.wzw.submitdata.utils;import Java.io.bytearrayoutputstream;import Java.io.inputstream;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.URL; public class Netutil {/** * uses get access to access the network * @param username * @param password * Results returned by @return server */public static String log Inofget (string username,string password) {httpurlconnection conn=null;try {String data= "username=" +username+ "& Password= "+password; URL url=new url ("Http://192.168.1.4:8080/AndroidServer/LoginServlet?") +data); conn= (httpurlconnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (10000) ; Conn.setreadtimeout (conn.connect); int Code=conn.getresponsecode (); if (code==200) {InputStream is= Conn.getinputstream (); String State=getstringfrominputstream (IS), return state;}} catch (Exception e) {e.printstacktrace ();} Finally{if (conn!=null) {conn.disconnect ();}} return null;} /** * Use post access to access the network * @param username * @param password * @return */public static string Loginofpost (string username,string password) {httpurlconnection conn=null;try {URL url=new url (" Http://192.168.1.4:8080/AndroidServer/LoginServlet "); conn= (httpurlconnection) url.openconnection (); Conn.setrequestmethod ("POST"); Conn.setconnecttimeout (10000); Conn.setreadtimeout (); Conn.setdooutput (true); /post requested parameter string Data= "Username=" +username+ "&password=" +password;outputstream out=conn.getoutputstream (); O Ut.write (Data.getbytes ()); Out.flush (); Out.close (); Conn.connect (); int Code=conn.getresponsecode (); if (code==200) { InputStream Is=conn.getinputstream (); String State=getstringfrominputstream (IS), return state;}} catch (Exception e) {e.printstacktrace ();} Finally{if (conn!=null) {conn.disconnect ();}} return null;} /** * Returns a string based on the input stream * @param is * @return * @throws Exception */private static string Getstringfrominputstream (InputStream is) throws Exception{bytearrayoutputstream baos=new Bytearrayoutputstream (); byte[] Buff=new byte[1024];int len=-1; while (Len=is.read (buff)) {!=-1) {baos.write (buff, 0, Len);} Is.close (); String html=baos.tostring (); Baos.close (); return html;}}