Android 76 submit data using get post

Source: Internet
Author: User

Get mode:

 PackageCom.itheima.getmethod;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportJava.net.URLDecoder;ImportJava.net.URLEncoder;Importcom.itheima.htmlviewer.utils.Utils;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } Handler Handler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {Toast.maketext (mainactivity. This, (String) msg.obj, 0). Show ();        }    };  Public voidClick (View v) {EditText et_name=(EditText) Findviewbyid (r.id.et_name); EditText Et_pass=(EditText) Findviewbyid (R.id.et_pass); FinalString name =Et_name.gettext (). toString (); FinalString pass =Et_pass.gettext (). toString (); Thread T=NewThread () {@Override Public voidrun () {//Submitted data needs to be URL encoded (UTF-8 encoded), English and digital encoding unchanged, if the browser will be automatically encoded, the client to manually code@SuppressWarnings ("Deprecation") String Path= "Http://192.168.13.13/Web2/servlet/LoginServlet?name=" +Urlencoder.encode (name)+ "&pass=" +Pass; Try{URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); Conn.setreadtimeout (5000); if(Conn.getresponsecode () = = 200) {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 (); }}
 Packagecom.itheima.htmlviewer.utils;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classUtils { Public StaticString Gettextfromstream (InputStream is) {byte[] B =New byte[1024]; intLen = 0; //creates a byte array output stream and reads the text data of the input stream, synchronously writes the data to the array output streamBytearrayoutputstream BOS =NewBytearrayoutputstream (); Try {             while(len = Is.read (b))! =-1) {Bos.write (b,0, Len); }            //converts data in a byte array output stream into a byte arrayString Text =NewString (Bos.tobytearray ()); returntext; } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return NULL; }}

Windowsmore encoding is GBK, Android default encoding is Utf-8.

Post mode:

 PackageCom.itheima.getmethod;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportJava.net.URLDecoder;ImportJava.net.URLEncoder;Importcom.itheima.htmlviewer.utils.Utils;ImportCOM.ITHEIMA.POSTMETHOD.R;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } Handler Handler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {Toast.maketext (mainactivity. This, (String) msg.obj, 0). Show ();        }    };  Public voidClick (View v) {EditText et_name=(EditText) Findviewbyid (r.id.et_name); EditText Et_pass=(EditText) Findviewbyid (R.id.et_pass); FinalString name =Et_name.gettext (). toString (); FinalString pass =Et_pass.gettext (). toString (); Thread T=NewThread () {@Override Public voidrun () {//the submitted data needs to be URL encoded, and the English and digital encoding will not change@SuppressWarnings ("Deprecation") String Path= "Http://192.168.13.13/Web2/servlet/LoginServlet"; Try{URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("POST"); Conn.setconnecttimeout (5000); Conn.setreadtimeout (5000); //to stitch up the string of data to be submittedString data = "Name=" + urlencoder.encode (name) + "&pass=" +Pass; //add two line properties for a POST requestConn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Conn.setrequestproperty ("Content-length", data.length () + ""); //set open output streamConn.setdooutput (true); //get the output stream on the client side like the serverOutputStream OS =Conn.getoutputstream (); //using the output stream to submit data to the server,Os.write (Data.getbytes ());//This time has not generated network interaction data                    if(Conn.getresponsecode () = = 200) {//Establish a connection to generate network interaction dataInputStream is = Conn.getinputstream ();//get the input stream returned by the serverString 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 (); }}

Android 76 submit data using get 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.