The use of HttpURLConnection in Android is five

Source: Internet
Author: User

In the third, I briefly introduce the get way to pass the data, and here I will briefly introduce the Post method. As to how the Get mode differs from post, I'll sell a xiaoguanzi first, and then I'll show you the difference and pros and cons of the two methods by pasting the two key codes.

Java code (first of all, the following code is the client code, that is, the phone-side code)

Get the code of the way (from the use of the third)

1     Private voidDoget ()throwsException2     {3LOG.I ("Main", "1");4url = url + "? name=" +name + "&age=" +Age ;5URL url =NewURL ( This. URL);6LOG.I ("Main", "2");7HttpURLConnection httpurlconnection =(HttpURLConnection) url.openconnection ();8Httpurlconnection.setrequestmethod ("GET");9Httpurlconnection.setreadtimeout (5000);Ten          One          A          -         //with GET, there is only one output stream on the server, so only one input stream is required on the phone side. -BufferedReader br =NewBufferedReader (NewInputStreamReader (Httpurlconnection.getinputstream ())); theStringBuffer SB =NewStringBuffer (); -String string =NULL; -          while(String = Br.readline ())! =NULL) -         { + sb.append (string); -         } +LOG.I ("main", sb.tostring ()); A}

Post Mode code

1     Private voidDopost ()throwsException2     {3URL url =NewURL ( This. URL);4HttpURLConnection httpurlconnection =(HttpURLConnection) url.openconnection ();5Httpurlconnection.setrequestmethod ("POST");6Httpurlconnection.setreadtimeout (5000);7         8         //Note that there is an output stream, because the use of the post transmission, there is an input stream on the server side, etc.9OutputStream out =Httpurlconnection.getoutputstream ();TenString content = "Name=" + name + "&age=" +Age ; One Out.write (Content.getbytes ()); A          -         //second, there is an output stream on the server side, so there must be an input stream, otherwise the thread will wait -BufferedReader br =NewBufferedReader (NewInputStreamReader (Httpurlconnection.getinputstream ())); theStringBuffer SB =NewStringBuffer (); -String string =NULL; -          while(String = Br.readline ())! =NULL) -         { + sb.append (string); -         } +LOG.I ("main", sb.tostring ()); A          at}

Finally, let me explain the difference between the Get and post methods:

The 1.get sends the data via a URL, so when the URL is initialized, the value of name and age is added, because the Get method sends the data through the URL, and the post sends the data through the output stream, which is the server-side input stream to receive the data.

The data sent in 2.get is relatively small, usually only a few kilobytes, and the post is usually used compared to a larger data, since post is the one that sends the data through the output stream.

When sending data in 3.get, all data is exposed through the URL and is not very secure, and post is sent through the flow channel, relatively safe

The use of HttpURLConnection in Android is five

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.