The HTTP protocol works: The client sends an HTTP request to the server, returns the data to the client after the server receives the request, and the client accepts the message and parses it.
There are two ways to send HTTP requests in Android, the first is through HttpURLConnection, and the second is through HttpClient.
Sending an HTTP request by means of httpurlconnection
This is usually divided into the following 5 steps:
1. Get the HttpURLConnection instance object. Start with a new URL instance, and then call the object's OpenConnection () method.
2. Set the method used by the HTTP request (get and post methods, the Get method is to fetch data from the server, post is to send data to the server).
3. Set parameters freely, such as connection timeout, read timeout, etc.
4. Call the getInputStream () method to get the information returned by the service.
5. Call the Disconnect () method to close the HTTP connection.
Sending an HTTP request by means of httpclient
HttpClient is an interface that Apache provides for HTTP network access.
1. Create an instance of Defaulthttpclient
2. Create a HttpGet object and pass in the destination network address, calling the Execute () method.
3. Get the return code to determine if the connection was successful. If successful, the corresponding data can be extracted.
Using the HTTP protocol to access the network in Android