[Android] POST request interface demo test code

Source: Internet
Author: User
Tags gettext

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

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.