Android Network application development, there are mainly two ways, one is socket (is the encapsulation of the TCP/UDP Protocol), the other is the use of HTTP protocol, Android is mainly provided in two ways, HttpURLConnection and Apache HttpClient. The following is a detailed description of the Android Network application development.
First, network communication based on socket
A socket is a mechanism for providing a network service. Both ends of the communication are sockets.
Network communication is actually the communication between the sockets.
The data is IO transmitted between two sockets.
1. Socket programming steps based on UDP
Define Dock
That is: Define a Datagramsocket object ds
Defines a container that can be used to receive or send data
That is: Define the Datagrampacket object DP
The container is used to accept the data sent over the dock (Ds.receive (DP))
Close Dock (Ds.close ())
2, TCP-based socket programming steps
II. network communication based on HTTP protocol
1, HttpURLConnection
The basic functionality of accessing the HTTP protocol is provided in the JDK's java.net package: HttpURLConnection. HttpURLConnection is the standard class of Java, HttpURLConnection inherits from URLConnection and can be used to send get requests, post requests to the specified Web site.
Here is a tool class that uses the HttpURLConnection class's GET request to access the network:
Path is the URL string
*
inputstream
/InputStream (string path) {
URL url =;
{
url = url (path);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setrequestmethod ();
Conn.setconnecttimeout ();
Conn.setdoinput ();
(Conn.getresponsecode () = =) {
InputStream in = Conn.getinputstream ();
In;
}
} (Malformedurlexception e) {
e.printstacktrace ();
} (IOException e) {
e.printstacktrace ();
}
;
}
2, Apache httpclient
The HTTP protocol is probably the most frequently used and important protocol on the Internet today, and more and more Java applications need to access network resources directly through the HTTP protocol. Although the basic functionality of accessing the HTTP protocol is already available in the JDK's Java net package, the JDK library itself offers not enough functionality and flexibility for most applications. HttpClient is a subproject under Apache Jakarta Common that provides an efficient, up-to-date, full-featured client-side Programming toolkit that supports HTTP protocols, and it supports the latest versions and recommendations of the HTTP protocol.
HttpClient is an enhanced version of what httpurlconnection,httpurlconnection can do httpclient all can do; httpurlconnection does not provide some functionality, HttpClient is also available, but it is focused on how to send requests, receive responses, and manage HTTP connections.
The following is a tool class to access the network using the HttpClient get method:
Stream * */[] (InputStream stream) ioexception{[] ret =;
Bytearrayoutputstream bout = Bytearrayoutputstream ();
[] buf = [];
Len
() {len = Stream.read (BUF); (len = =-)
{
;
} bout.write (buf, Len);
} BUF =;
ret = Bout.tobytearray ();
Bout.close ();
Ret
URL * * */[] (String URL) {[] ret =;
HttpGet request = HttpGet (URL);
HttpClient client = Defaulthttpclient ();
{HttpResponse response = Client.execute (request);
Statusline statusline = Response.getstatusline ();
StatusCode = Statusline.getstatuscode (); (StatusCode = =)
{httpentity entity = response.getentity ();
InputStream stream = Entity.getcontent (); ret = Readstream (stream);
Stream.Close ();
} (IOException e) {e.printstacktrace ();
RET; }