[Android] post request interface demo test code, androiddemo

Source: Internet
Author: User

[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>

 

Related Article

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.