Android Learning (49)--submit data using GET method

Source: Internet
Author: User

The data submitted by the Get method is directly stitched at the end of the URL
Final String Path = "Http://192.168.1.103/Web/servlet/CheckLogin?name=" + name + "&pass=" + Pass;

Send a GET request, the code is the same as before
URL url = new URL (path); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestmethod ("GET"); conn.setreadtimeout (conn.setconnecttimeout); if (conn.getresponsecode () = = 200) {}

When the browser sends the request carries the data the data URL encodes, we also need to write the code in the Chinese URL encoding urlencoder.encode (name), is outdated, the code is UTF-8, this is more convenient,
Urlencoder.encode (name, "Utf-8"), this is not obsolete, if the encoding is not utf-8 need to use this.

String Path = "Http://192.168.1.103/Web/servlet/CheckLogin?name=" + urlencoder.encode (name) + "&pass=" + Pass;

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?name=" + urlencoder.encode (name) + "&pass=" + pass;try {URL url = new URL (path); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (conn.setreadtimeout); 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 (49)--submit data using GET method

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.