Complete analysis of android Network Application Development

Source: Internet
Author: User

Complete analysis of android Network Application Development

Zookeeper Android network application development mainly involves two methods: socket (encapsulation of tcp/udp Protocol) and Http protocol, android provides two methods: HttpURLConnection and Apache HttpClient. This section describes Android network application development in detail.

1. socket-Based Network Communication 1. udp-based socket programming steps

Define a pier
That is, define a initramsocket object ds

Define the container that can be used to receive or send data
That is, the DatagramPacket object dp is defined.

Receiving data from objects in containers on the terminal (ds. receive (dp ))

Close dock (ds. close ())

2. tcp-based socket programming steps

2. Use HTTP to Access Network Resources 1. HttpURLConnection

Zookeeper JDK's java.net package provides the basic HTTP access function: HttpURLConnection. HttpURLConnection is a standard java class. HttpURLConnection inherits from URLConnection and can be used to send GET and POST requests to a specified website.

The following is a get request using the HttpURLConnection class to access the network:

/*** The getInputStream function is used to obtain the byte input stream for reading server resources * @ param path is the url String * @ return InputStream */public static InputStream getInputStream (String path) {URL url = null; try {url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); // set the Request Method conn. setRequestMethod (GET); // set the connection timeout value conn. setConnectTimeout (5000); // you can set to read data from the server. setDoInput (true); // get the server response code if (conn. getResponseCode () = 200) {// get the byte input stream InputStream in = conn. getInputStream (); return in ;}} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return null ;}
2. Apache HttpClient

Zookeeper HTTP protocol may be the most widely used and important protocol on the Internet. More and more Java applications need to directly access network resources through HTTP protocol. Although the basic functions for accessing HTTP are provided in the JDK java net package, for most applications, the functions provided by the JDK library itself are not rich and flexible. HttpClient is a sub-project under Apache Jakarta Common. It is used to provide an efficient, up-to-date, and function-rich client programming toolkit that supports HTTP protocol. It also supports the latest versions and suggestions of HTTP protocol.

  HttpClient is an enhanced version of HttpURLConnection. HttpURLConnection can do all the things HttpClient can do. HttpURLConnection does not provide some functions, and HttpClient also provides, however, it only focuses on how to send requests, receive responses, and manage HTTP connections.

The following is a tool class for using httpClient get to access the network:

/*** Read the input stream and return data * @ param stream * @ return */public static byte [] readStream (InputStream stream) throws IOException {byte [] ret = null; byteArrayOutputStream bout = new ByteArrayOutputStream (); byte [] buf = new byte [128]; int len; while (true) {len = stream. read (buf); if (len =-1) {break;} bout. write (buf, 0, len);} // force release of temporary resource buf = null; ret = bout. toByteArray (); bout. close (); return ret;}/*** get Request Method * @ param url * @ return */public static byte [] doGet (String url) {byte [] ret = null; HttpGet request = new HttpGet (url); HttpClient client = new DefaultHttpClient (); try {HttpResponse response = client.exe cute (request ); statusLine statusLine = response. getStatusLine (); int statusCode = statusLine. getStatusCode (); if (statusCode = 200) {HttpEntity entity = response. getEntity (); InputStream stream = entity. getContent (); // The actual returned data ret = readStream (stream); stream. close () ;}} catch (IOException e) {e. printStackTrace ();} return ret ;}
3. Differences between HttpURLConnection and HttpClient

HttpClient has fewer bugs before zookeeper Android 2.2, so using it is the best choice. In Android 2.3 and later versions, HttpURLConnection is the best choice. New applications should be more inclined to use HttpURLConnection.

 

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.