Using the HTTP protocol to access the network in Android

Source: Internet
Author: User

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

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.