On Android Network programming

Source: Internet
Author: User

Android practitioners, ten are doing network applications, not the Internet is also a local area network. Now in the world of 4G and WiFi, traffic is already floating clouds, single-use market has been quietly gone. So, do not understand the network request students to be careful, beware of the waves of the times a wave row on the beach.

Android enables network programming with HTTP and sockets. HTTP protocol is the application layer protocol, mainly solves how to wrap the data, the network application will use the Protocol; socket is the TCP/IP protocol encapsulation, mainly to solve the data how to transport in the network, often used to maintain long connection with the server, generally used for advertising push, real-time chat, online games and so on.

If your scope of work does not involve these requirements, you generally do not have a socket. Our topic today is the implementation of HTTP requests in Android. HTTP request method has a lot, get,post,head,delete, etc., commonly have get, POST. In general, these two request methods are sufficient to solve the work needs, get used to query the request, post is used to commit the modification request. Interested students can also go online to check information, learn more about the HTTP request data formatting, response code, and recently the new HTTP protocol standards have been released, you can look at.

The following is said to get and post these two request ways of similarities and differences, understanding of the interface to facilitate the call, in fact, is mainly the background of the classmate for us to define the good. Simply understood, get is to get data to the server, post is to send data to the server, get request is the request parameter in the form of visible stitching to the URL, in fact, is encapsulated in the header, security is low, POST request is to put the request parameters into the body, Users do not see the process of execution, high security, get transmission data length is limited, also 2KB, so can not be used to upload files, and post requests in principle there is no limit, but considering the stability of the network and the security of the server, generally also set the length of the upload file threshold, so commonly used to upload files. In fact, get and post have something in common, for example, you can cache the request by setting the Expires property.

We implement HTTP requests in Android, and native support is implemented in two ways. One is the javase built-in Network request class, one is the adoption of Apache HttpClient. In Java, there is a network-related package java.net.*, which encapsulates a series of classes that need to be used in connection with network requests, such as HttpURLConnection and URLs, and encapsulates a request tool class based on these two classes; the other is httpclient, is widely used by Java developers, has a simpler API and more efficient, so it was adopted by Google in the Android API, relative to HttpURLConnection I also wrote a tool class, the last tool class of all the methods have been re-implemented again. These two tools are basically able to meet the needs of daily work development, and everyone is interested to pull on GitHub.

The following is a brief explanation of the role of these two network Operation tool classes. First look at the Javadoc generated method summary, because the method of two classes is the same, only for Apachehttputil. Considering the multiple invocations of the tool class, all methods are declared static for convenience and efficiency.

A GET request has two methods, one with no request parameters, and one to satisfy the parameter. The encapsulation of parameters is uniformly used in the form of map key-value pairs, which is easier to understand and more efficient to use map. Apachehttputil will be assembled automatically, you just have to send your request parameters in the form of a map, and then successfully get the string results returned by the server.

The POST request is the same as the external parameters, first the map collection to the Namevaluepair list, and then plug into the HTTP entity body inside, and then successfully get the string results returned by the server.

The last method is to upload multiple files and submit text parameters while uploading files. Only need to pass the value over the text parameter list, the file local path address list, so even if it is done, HttpClient will help us encapsulate everything, it is much more useful than Java native. If you want to see the problems caused by native people, please refer to the implementation of Nativehttputil in this class.

Source Address: Https://github.com/JackCho/AndroidHttpUtil

Okay, here's the end of the share. Help a friend ad, is an Android Daniel, young he gave up the 30W annual salary of the generous treatment, chose their own entrepreneurial full-time to do Android education training.

For more information, please visit the annual salary of 300,000, why should I leave? "http://www.cnblogs.com/stay/p/3859211.html

If you find it helpful, you are welcome to subscribe to my public account-Android dry sharing . Below is the QR code, to provide you with timely high-quality Android dry.

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.