Android uses the HTTP protocol to access the network

Source: Internet
Author: User

The HTTP protocol works roughly as follows: The client sends an HTTP request to the server, the server returns some data to the client after the request is received, and the client parses the data.

Before Android6.0, there were two main ways of sending HTTP requests on Android: HttpURLConnection and HttpClient. HttpClient There are too many APIs and difficult to expand, so in the Android6.0 system, HttpClient is completely removed, if you want to use, you need to import the appropriate files. Here is a brief introduction to the basic usage of the httpurlconnection I recently studied, followed by a current popular network communication library Okhttp.

Android obtains the network data mainly has the get and the post method, the get represents obtains the data from the server, the post represents submits the data to the server.

1. First create a new Httpurlconnectiondemo file, add two buttons, respectively, using the Get method and the Post method.

2. Add a Click event for the button to execute the Doget and Dopost methods respectively.

    • GET request

Note that the method here must be executed in a child thread, or the runtime will report a android.os.NetworkOnMainThreadException error indicating that the network request cannot be performed in the main thread.

    • POST request

Of course, the most important point, before accessing the network, you need to declare network permissions, modify the code in the Androidmanifest.xml,

After running the code, you can get the corresponding data.

It can be seen that the use of httpurlconnection is cumbersome, we can use the current popular Okhttp network communication library instead of it. Okhttp is developed by the famous square company and is easy to use. Beginners can understand their network access process through HttpURLConnection, but in the actual project development process, using more than the Okhttp,okhttp project home address is: http://github.com/square/okhttp.

1. Before using okhttp, you need to add a dependency in your project to find the latest version number of the current okhttp by accessing the Okhttp project home address.

2. Then click on the top right corner to complete the synchronization. Create a new project, which is modified on the previous Httpurlconnextiondemo basis.

3. To affirm a okhttpclient on the basis of Httpurlconnextiondemo,

4. Get the data using two requests, where the GET request uses the synchronous request method, and the post request uses the method of the asynchronous request callback.

    • GET request

    • POST request

Android uses the HTTP protocol to access the network

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.