An explanation of the Android network access method

Source: Internet
Author: User

Most Android applications use the HTTP protocol to send and receive network data, while Android provides two main ways to perform HTTP operations.

HttpURLConnection and HttpClient. Both of these methods support HTTPS protocol, upload and download in streams, configure timeout, IPv6, and connection pooling.

One: First look at the network access related APIs available under Android

A) java.net.* (Standard Java interface): Includes streaming and packet (datagram) sockets, Internet protocol, and common HTTP processing. HttpURLConnection is under this bag.

b) The open source package provided by Org.apache.HttpClient:Appache, which compensates for the shortcomings of Java.net.* 's lack of flexibility, defaulthttpclient is its specific implementation class.

c) android.net (Android interface): It is also the implementation of HttpClient, which provides unique network programming under Android. Such as: Access to WiFi, access to Android Internet Information, mail and other functions.

Two: Which client is the best?

Google official documents, in 2.3 and later versions, HttpURLConnection is the best choice, its simple API and lightweight very suitable for Android. The compression and caching mechanism reduces network usage, improves speed, and saves power. The new app should choose to use httpurlconnection, but there are bugs before 2.2.

But in terms of link management and thread safety. HttpClient still has a big advantage, providing a large number of scalable and stable APIs, but it is difficult to modify, upgrade, and extend many of its APIs without compromising its compatibility. Google's optimizations are not positive and are generally used in android2.2 and the following versions.

Three: Network protocol

The network seven layer, the general programmer touches most is the application layer and the transport layer.

Layers in the OSI

Layers in the OSI

TCP/IP protocol family

Application Layer

File transfer, email, file service, virtual terminal

Tftp,http,snmp,ftp,smtp,dns,telnet

Presentation Layer

Data formatting, code conversion, data encryption

Session Layer

To release or establish contact with another contact.

Transport Layer

Provides an end-to-end interface

Tcp,udp

Network layer

To select a route for a packet

Ip,icmp,rip,ospf,bgp,igmp

Data Link Layer

Transmit address-enabled frames and error detection

Slip,cslip,ppp,arp,rarp,mtu

Physical Layer

Transfer data on physical media in binary data form

iso2110,ieee802. IEEE802.2

TCP and UDP are transport layer protocols that define specifications for data transfer and connection methods (connection-oriented, TCP three-layer handshake) (UDP unreliable protocol)

HTTP is the application-layer protocol, which defines the specification of the content of the transmitted data (each request sent by the client requires a server loopback response, and the connection is actively released after the request has ended)

A socket is a set of programming interfaces (APIs). Provides a unified programming interface to the application layer in both the transport and application tiers. The application layer does not need to know the TCP/IP protocol details. The transmission of the data over the IP network is done directly through the invocation of the socket interface function.

TCP-based sockets (HTTP), UDP-based sockets (SNMP), RAWIP-based sockets (pings), link-layer-based sockets

The data in the HTTP protocol is transmitted using the TCP protocol, so support for HTTP will also support TCP

When we transmit data, we can only use the (Transport Layer) TCP/IP protocol, but then, if there is no application layer, the data content is not recognized, if you want to make the transferred data meaningful, you must use the Application layer protocol, the application layer protocol, such as HTTP, FTP, Telnet, etc., also You can define the application layer protocol yourself. The web uses the HTTP protocol as an application-layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol.

IV: Other network-related technologies

1) webview:html<------->javascript<-------->android Java

2) Webservice: Based on SOAP protocol, Simple Object Access protocol, Simple Object Access Protocol (SOAP) is a lightweight, simple, XML-based protocol.

3) Server push: Through the establishment of a persistent connection method, the server sends information to the mobile phone Android users.

--MQTT protocol

--XMPP protocol

--third party push Android push Notification Service

4) PhoneGap

An explanation of the Android network access method

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.