Mainactivity.java
Packagecom.tsh.test;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity { PublicButton loginbtn; PublicTextView Loginusername; PublicTextView Loginpassword; Public StaticString api= "Http://mail.sina.net/loginxxx"; PublicLoginhandler Loginhandler; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Get View Objectloginbtn=(Button) Findviewbyid (R.ID.LOGINBTN); Loginusername=(TextView) Findviewbyid (r.id.loginusername); Loginpassword=(TextView) Findviewbyid (R.id.loginpassword); //set a Click event for a View objectLoginbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//Open new ThreadThread loginthread=NewThread (Newloginrunable ()); Loginthread.start (); } }); Loginhandler=NewLoginhandler (); } //implement Runable interface, open new thread classLoginrunableImplementsrunnable{@Override Public voidrun () {Try{URL URL=NewURL (API); HttpURLConnection http=(HttpURLConnection) url.openconnection (); Http.setrequestmethod ("POST"); Http.setdoinput (true); Http.setdooutput (true); OutputStream Ops=Http.getoutputstream (); PrintWriter PW=Newprintwriter (OPS); String username=Loginusername.gettext (). toString (); String Password=Loginpassword.gettext (). toString (); Pw.write ("Email=" +username+ "&psw=" +password+ "&loginfrom=app&output=json"); Pw.flush (); InputStream ins=Http.getinputstream (); byte[] buffer =New byte[1024]; intLength=0; StringBuilder SB=NewStringBuilder (); while(Length=ins.read (buffer))!=-1) {sb.append (NewString (buffer,0, length)); } Message msg=NewMessage (); Msg.what=1; Msg.obj=sb.tostring (); Loginhandler.sendmessage (msg); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } //handle of passing messages classLoginhandlerextendshandler{@Override Public voidhandlemessage (Message msg) {String loginresponse=(String) msg.obj; System.out.println (Loginresponse); Toast.maketext (mainactivity. This, Loginresponse, 10). Show (); Intent Intent=NewIntent (mainactivity. This, Mailindexactivity.class); //startactivity (intent); } }}
Main_activity.xml
<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:orientation= "Vertical"Tools:context= "${relativepackage}.${activityclass}" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "User name"/> <EditText Android:hint= "Please enter user name"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/loginusername"Android:text= "[email protected]"/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Password"/> <EditText Android:hint= "Please enter password"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/loginpassword"Android:text= "xxxxxxx"/> <Button Android:id= "@+id/loginbtn"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Login Authentication"/></linearlayout>
[Android] POST request interface demo test code