Android Network Programming HttpClient Socket

Source: Internet
Author: User



Currently the Android platform has 3 network communication interfaces:

1, java.net.* (Standard Java Interface): Urlconnection/httpurlconnection class.

2, Org.apache (Apache interface): HttpClient interface

3. Android.net (Android network Interface): An HTTP programming interface implemented via the encapsulation of Apache's httpclient interface.

One, the standard Java interface:

There are generally two ways to request http: Get and Post

Get method: Get request can get a static page, or you can put parameters after the URL string, passed to the server.

The difference between post and get is that the post is an item parameter placed in the HTTP request data.

Network communication under the standard Java interface requires the aid of the HttpURLConnection abstract class, which inherits from the URLConnection abstract class.

Since both HttpURLConnection and URLConnection are abstract classes, they cannot be instantiated, and their objects are obtained through the OpenConnection method.

The code to create a httpurlconnection connection is as follows:

URL url = new URL ("http://www.google.com");

HttpURLConnection Urlcon = (httpurlconnection) url.openconnection ();

The OpenConnection method creates only URLConnection or httpurlconnection instances, but does not perform a true connection operation.

And a new instance will be created after each openconnection.

Here are some property settings:

Connection.setdooutput (TRUE)//Set input/output stream

Connection.setdoinput (TRUE);

Set Request mode

Connection.setrequestmethod ("POST");
Connection.setusecaches (FALSE)//Set POST request cannot use cache

Connection.disconnection ();//release connection.

Second, Apache interface: HttpClient interface

The HttpClient interface is a layer of encapsulation of the java.net interface and is more suitable for Android development networking applications.

How to get HttpClient:

HttpClient client = new Defaulthttpclient (); Defaulthttpclient is the default HTTP client. It can create an HTTP connection.

HttpResponse is an HTTP connection response that, when an HTTP request is executed, returns a httpresponse that can be used to obtain some response information.

HttpResponse HttpResponse = Client.execute (HttpRequest);

The incoming request method is required when executing httpclient: GET, Post

Get:httpget HttpRequest = new HttpGet (URL);

Psot:httppost HttpRequest = new HttpPost (URL);

The Post method requires that the request parameters be saved to Namevaluepair.

if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//success}


Socket communication:

There are two main ways to operate a socket: connection-oriented and non-connected.

Connection-oriented operation (using TCP protocol): Like a radio station, you must establish a connection and a call

Advantages: safe and reliable.

OK: low transmission efficiency.

No connection mode of operation (UDP protocol): Like mail delivery, there is no guarantee.

Advantages: High transmission efficiency.

OK: poor reliability.

Socket Programming Principle:

By using the two classes provided in the Java.net package: Sockets and ServerSocket: respectively, the client and server that represent the two-way connection.

Client: Socket:socket socket= newsocket (ip,port);//parameter is the IP and port number of the server.

To create a client using a socket, follow these steps:

1. Instantiate the socket via IP address and port to request a connection to the server.

2. Get the stream on the socket to read and write.

3, the flow of packaging into the bufferreader/printwriter example

4. Read and write the socket

5. Close the open stream.

Server: ServerSocket server = new ServerSocket (port);//ports for server.

The steps to create a server using ServerSocket are as follows:

1. Specify a port to instantiate a ServerSocket

2. Call the Accept () method of ServerSocket to cause blocking during the wait.

3. Get the stream that is located on the underlying socket read and write operations

4. Encapsulate data into a stream

5. Read and write the socket

6. Turn off the open stream.

Android Network Programming HttpClient Socket

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.