[Android] post request interface demo test code, androiddemo
MainActivity. java
Package com. tsh. test; import java. io. inputStream; import java. io. outputStream; import java. io. printWriter; import java.net. httpURLConnection; import java.net. URL; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. Widget. textView; import android. widget. toast; public class MainActivity extends Activity {public Button loginBtn; public TextView loginUserName; public TextView loginPassword; public static String API = "http://mail.sina.net/loginxxx"; public LoginHandler loginHandler; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity _ Main); // obtain the View object loginBtn = (Button) findViewById (R. id. loginBtn); loginUserName = (TextView) findViewById (R. id. loginUsername); loginPassword = (TextView) findViewById (R. id. loginPassword); // set the Click Event loginBtn for the View object. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// enable the new Thread loginThread = new Thread (new LoginRunable (); loginThread. start () ;}}); loginHandler = new Log InHandler () ;}// implement the Runable interface to enable the new Thread class LoginRunable implements Runnable {@ Override public void run () {try {URL url = new URL (API ); httpURLConnection http = (HttpURLConnection) url. openConnection (); http. setRequestMethod ("POST"); http. setDoInput (true); http. setDoOutput (true); OutputStream ops = http. getOutputStream (); PrintWriter pw = new PrintWriter (ops); String username = loginUserName. getText (). toStri Ng (); 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]; int length = 0; StringBuilder sb = new StringBuilder (); while (length = ins. read (buffer ))! =-1) {sb. append (new String (buffer, 0, length);} Message msg = new Message (); msg. what = 1; msg. obj = sb. toString (); loginHandler. sendMessage (msg);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}}// handle class LoginHandler extends Handler {@ Override public void handleMessage (Message msg) {String loginResponse = (String) msg that transmits messages. obj; System. out. println (loginResponse); Toast. makeText (MainActivity. this, loginResponse, 10 ). show (); Intent intent = new Intent (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 =" username "/> <EditText android: hint = "Enter the user name" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/loginUsername" android: <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "password"/> <EditText android: hint = "enter the 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>