Talk about three network communication modes of Android

There are three network interfaces available for the Android platform: Java. Net. * (standard Java interface), org. Apache interface and Android. Net. * (Android network interface). The functions and functions of these interfaces are described below.

1. Standard Java interface

Java. Net. * provides networking related classes, including stream, packet socket, internet protocol, common HTTP processing, etc. For example, create URLs, urlconnection / httpurlconnection objects, set link parameters, link to the server, write data to the server, read data from the server, etc. These are involved in Java network programming. Let's look at a simple socket programming to realize the server sending back client information.

Server:

Client, mainactivity

Layout file:

Start server:

Run the client program:

The results are shown in the figure below:

Note: possible reasons why the server and client cannot be linked are:

No permission to access the network: < uses permission Android: name = "Android. Permission. Internet" > < / uses Permission >

IP address to use: 10.0.2.2

The simulator cannot configure the agent.

2。 Apache interface

For most applications, the network function provided by JDK itself is far from enough. At this time, Apache httpclient provided by Android is needed. It is an open source project with more perfect functions. It provides efficient, up-to-date and feature rich toolkit support for client-side HTTP programming.

Let's take a simple example to see how to use httpclient to access the web on Android client.

First, to build a web application myapp on your machine, there is only a very simple http.jsp

The contents are as follows:

Then implement the Android client to access myapp by post and get. The code is as follows:

Layout file:

Resource file:

strings.xml

Primary activity:

The operation results are as follows:

3. Android.net programming:

The classes under this package are often used for Android specific network programming, such as accessing WiFi, accessing Android networking information, email and other functions. I won't go into detail here.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>