HTTP Communication for Android Development
Httpclient Interface
/*
* HTTP Communication httpclient interface developed for Android
*
Beijing Android Club group: 167839253
* Created on: 2012-5-10
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
Httpclient Interface
Apache provides the httpclient interface, which encapsulates and abstracts classes in java.net. It is more suitable for developing online applications on Android. To use httpclient, you also need to know some classes:
1.
Clientconnectionmanager Interface
This interface is used by the client to connect to the browser and provides the following Abstract METHODS:
Public Methods |
Abstract void |
Closeexpiredconnections ()// Close all invalid timeout connections Closes all expired connections in the pool. |
Abstract void |
Closeidleconnections (long idletime, timeunit tunit)// Close idle connections Closes idle connections in the pool. |
Abstract schemeregistry
|
Getschemeregistry ()// Get a schemeregistry Obtains the scheme registry used by this manager. |
Abstract void |
Releaseconnection (managedclientconnection Conn, long validduration, timeunit)
Releases a connection for use by others. // release a connection |
Abstract clientconnectionrequest
|
Requestconnection (httproute route, object state )// Request a new connection Returns a new clientconnectionrequest, from which a managedclientconnection can be obtained or the request can be aborted. |
Abstract void |
Shutdown () // Close the manager and release the resource Shuts down this Connection Manager and releases allocated resources. |
2.
Defaulthttpclient
Defaulthttpclient is the default HTTP client that can be used to create an HTTP connection.
This class replaces httpclient in httpclient 3.
The code for creating a defaulthttpcolient is as follows:
Httpclient = new defaulthttpclient ();
3.
Httpresponse
Httpresponse is an HTTP connection response. After an HTTP connection is executed, an httpresponse is returned. You can obtain some response information through httpresponse. For example, the code that requests an HTTP connection and obtains whether the request is successful:
Httpclient = new defaulthttpclient ();
Httpresponse = httpclient.exe cute (httprequest );
If (httpresponse. getstatusline (). getstatuscode () = httpstatus. SC _ OK ){
// Connection successful
}
The get and post code will be attached later. The code is basically similar to the httpurlconnection code.