Android Learning (50)--submit data using Post

Source: Internet
Author: User
Tags gettext

Important Post submission data is two more properties in the protocol header that is written to the server by the stream Content-type: application/x-www-form-urlencoded, describing the mimetype of submitted data
Content-length: 30, describing the length of the submitted data

Add post extra two properties to the request header string data = "Name=" + urlencoder.encode (name) + "&pass=" + pass;conn.setrequestproperty (" Content-type "," application/x-www-form-urlencoded "); Conn.setrequestproperty (" Content-length ", data.length () +" ");

set the stream that allows the post request to be opened
Conn.setdooutput (TRUE);
gets the output stream of the connection object, writes the data to be submitted to the server in the stream
OutputStream OS = Conn.getoutputstream (); Os.write (Data.getbytes ());


Core code
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {Toast.maketext (Mainactivity.this, ( String) msg.obj, 0). Show ();}}; public void Click (View v) {EditText et_name = (EditText) Findviewbyid (r.id.et_name); EditText Et_pass = (EditText) Findviewbyid (R.id.et_pass); final String name = Et_name.gettext (). toString (); final string pass = Et_pass.gettext (). toString (); Thread t = new Thread () {@Overridepublic void Run () {//Submitted data needs to be URL encoded, English and digitally encoded unchanged @suppresswarnings ("deprecation") String Path = "Http://192.168.13.13/Web2/servlet/LoginServlet"; try {URL url = new URL (path); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestmethod ("POST"); Conn.setconnecttimeout (Conn.setreadtimeout (5000);//String data = "Name=" + urlencoder.encode ( Name) + "&pass=" + Pass;//add two lines of the POST request Property Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Conn.setrequestproperty ("Content-length", data.length () + "");//set Open output stream Conn.setdooutput (true);//Get output stream OutputStream OS = Conn.getoutputstream ();//Use the output stream to submit data to the server Os.write (Data.getbytes ()); if (conn.getresponsecode () = =) {InputStream is = Conn.getinputstream (); String Text = Utils.gettextfromstream (IS); Message msg = Handler.obtainmessage (); msg.obj = Text;handler.sendmessage (msg);}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}; T.start ();}}


Gettextfromstream
public static String Gettextfromstream (InputStream is) {byte[] b = new Byte[1024];int Len = 0;//creates a byte array output stream, reads the text data of the input stream, synchronizes the number Writes an array output stream bytearrayoutputstream Bos = new Bytearrayoutputstream (); try {while (len = Is.read (b))! =-1) {bos.write (b, 0, Len) ;} Converts the data in the byte array output stream into a byte array, string text = new String (Bos.tobytearray ()); return text;} catch (IOException e) {e.printstacktrace ();} return null;}


Android Learning (50)--submit data using Post

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.