Android Network application Development detailed

Source: Internet
Author: User
Tags socket

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; }
Related Article

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.