Android Network Programming HttpClient Socket
Zookeeper
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 by encapsulating the HttpClient interface of Apache.
I. standard java interfaces:
There are two methods for Http requests: get and post.
Get method: get requests can obtain static pages, or put parameters after the URL string and pass them to the server.
The difference between post and get is that post is the item parameter placed in the http request data.
The network communication under the standard java interface requires the use of the HttpURLConnection abstract class, which inherits from the URLConnection abstract class.
Because both HttpURLConnection and URLConnection are abstract classes, they cannot be instantiated and their objects are obtained through the openConnection method.
The code for creating an HttpURLConnection connection is as follows:
URL url = new URL ("http://www.google.com ");
HttpURLConnection urlCon = (HttpURLConnection) url. openConnection ();
The openConnection method only creates URLConnection or HttpURLConnection instances, but does not actually perform connection operations,
A new instance is created after each openConnection operation.
Some attribute settings are as follows:
Connection. setDoOutput (true) // sets the input/output stream.
Connection. setDoInput (true );
Set Request Method
Connection. setRequestMethod ("POST ");
Connection. setUseCaches (false) // set the post request to not use the cache
Connection. disconnection (); // release the connection.
Ii. Apache interface: HttpClient Interface
The HttpClient interface encapsulates the java.net interface and is more suitable for developing online apps for Android.
How to obtain HttpClient:
HttpClient client = new DefaultHttpClient (); // DefaultHttpClient is the default http client. You can create an HTTP connection.
HttpResponse is an Http connection response. After an HTTP request is executed, an HttpResponse is returned. You can obtain some response information through HttpResponse.
HttpResponse httpResponse = client.exe cute (httpRequest );
When executing HttpClient, You need to input the Request Method: get, post
Get: HttpGet httpRequest = new HttpGet (url );
Psot: HttpPost httpRequest = new HttpPost (url );
In post mode, you need to save the request parameters to NameValuePair.
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// succeeded}
Socket communication:
There are two main methods to operate a Socket: connection-oriented and connectionless.
Connection-oriented operation (using TCP protocol): Like a radio station, a connection and a call must be established.
Advantages: secure and reliable.
OK: low transmission efficiency.
Non-connection operation method (UDP protocol): Like mail delivery, not guaranteed.
Advantage: high transmission efficiency.
OK: poor reliability.
Socket programming principle:
The two classes Socket and ServerSocket provided in the java.net package are used to represent the client and server with two-way connection respectively.
Client: Socket socket = newSocket (IP, port); // The parameter is the IP address and port number of the server.
To create a client using a Socket, follow these steps:
1. instantiate the Socket through the IP address and port and request to connect to the server.
2. Obtain the stream on the Socket for read/write.
3. Package the stream into the BufferReader/PrintWriter instance.
4. Read and Write the Socket
5. Close the opened stream.
Server: ServerSocket server = new ServerSocket (port); // The port of the server.
To create a server using ServerSocket, follow these steps:
1. Specify a port to instantiate a ServerSocket
2. Call the ServerSocket accept () method to cause blocking during waiting.
3. Read and Write operations have been performed to obtain the stream located in the underlying Socket.
4. encapsulate data into a stream
5. Read and Write the Socket
6. close and open the stream.