Android Post way to send data to background server

Source: Internet
Author: User

The simple difference between the request mode get and post:

The Get method is to append parameters to the URL address, such as the following:

http://localhost:8080/loginServlet.html?username=123&password=456

Post is to put the request parameters into the request body, stream to the server, in addition to upload files, must be the post mode


The following code simulates a user login by using the Post method

Com.yuanlp.qqloginpostandroid.os.Bundleandroid.support.v7.app.AppCompatActivityandroid.text.TextUtilsandroid.view.Viewand Roid.widget.Buttonandroid.widget.CheckBoxandroid.widget.EditTextandroid.widget.Toastorg.json.JSONExceptionorg.json.JSONOb Jectjava.io.BufferedReaderjava.io.InputStreamjava.io.InputStreamReaderjava.net.HttpURLConnectionjava.net.URLjava.net.URLE Ncodermainactivity appcompatactivity {    ==edittext edittext checkbox  Button String String  (bundle savedinstancestate)  {         .oncreate (savedinstancestate) Setcontentview (r.layout.) =  (EditText)  findviewbyid (r.id.) =  (EditText)  findviewbyid (r.id.) =  (CheckBox)  findviewbyid (r.id.) =  (Button)  findviewbyid (r.id.)}      (View view) {        = .gettext (). ToString (). Trim () = .gettext (). ToString (). Trim () (Textutils. (). TexTutils. ()) {            toast. (Toast.). Show ()}        .setenabled () Thread () {              ()  {                 {                     thread. () String path=url url = url (path) httpurlconnection conn =  ( httpurlconnection)  url.openconnection () Conn.setrequestmethod () Conn.setconnecttimeout () Conn.setrequestproperty () String data=+ urlencoder. () ++urlencoder. () Conn.setrequestproperty (String. ( Data.length ())) Conn.setdooutput () Conn.getoutputstream (). Write (Data.getbytes ()) code =  Conn.getresponsecode () (code==) {                 &nbsP;       inputstream is=conn.getinputstream () BufferedReader br= BufferedReader (InputStreamReader (IS)) Stringbuffer sb=stringbuffer () string len= ((Len=br.readLine ())! = ) {                             sb.append (len)}                          string result=sb.tostring () runtoastanythread (Result)}                 }  (exception e)  {                      E.printstacktrace ()}            }         }.starT ()}     (String result)  {         Runonuithread (Runnable ()  {            ()  {                JSONObject  jsonobject={                    jsonobject=jsonobject () String code=jsonobject.get (). ToString () (. Equals (code)) {                        toast. (Getapplicationcontext () Toast.). Show ()} (. Equals (code)) {                        toast. (Getapplicationcontext () Toast.). Show ()}               }  (JSOnexception e)  {                    e.printstacktrace ()}             }        })}}


This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1941138

Android Post way to send data to background server

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.