Method of building web server on Android device

Generally speaking, Android applications make requests to remote servers by means of get or post when requesting data. Have you ever thought that we can also build a small web server on Android devices and realize conventional functions such as downloading pictures, downloading files and submitting forms?

The following is how to build a web server on Android devices. The functions of this web server are as follows:

This depends on an open source library: andserver

Andserver is similar to Apache and Tomcat. It supports devices under the same LAN. It can request data from the web server in the conventional network request mode, as long as it indicates the IP address and port number of the web server

So what are the uses of this web server?

Let's talk about a demand I meet now! Two devices (Android or IOS devices) need to communicate data without network. It was originally intended to link the communication channels between devices with WiFi, that is, one device is connected to the wiif hotspot of another device, so a "channel" is established between the two. Then, the socket connection is established to obtain the input-output stream and exchange data through the input-output stream. However, in this way, it is troublesome to deal with the problem of data synchronization and analysis in the case of high concurrency. Through andserver, you can directly apply the existing network request framework of the project to exchange data directly in the form of network request, and the server also handles the concurrency problem better

Gradle remote dependency

Build server

The way to build the server is very simple and supports chain calls. Indicates that the server listens on the local IP address, specifies the port number as 1995, and opens three interfaces for downloading files, downloading pictures, and post forms

Turn on the server

Stop server

Interface processor

When registering an interface, in addition to indicating the open URL address, you also need to indicate the corresponding processor. The three open interfaces correspond to three addresses respectively

For example, suppose that the IP address of the device is 192.168.0.101, then access http://192.168.0.101:1995/file Interface, the request operation will be handled by downloadfilehandler

Download File

Downloadfilehandler implements the requesthandler interface. In the handle method, you can obtain the request header, form data and other information. It supports the call of get mode through annotation declaration. Here, a text file is directly returned for download

Here, you can directly access the interface in the browser (running on the same LAN as the Android web server) and download it to the file directly

Download pictures

Similarly, the interface processor downloadimagehandler for downloading images can be designed as follows to return the application icon in the handle method

@H_ 502_ 100@

Post form

Here, you need to change the annotation value to requestmethod.post. You can obtain the form data through the httprequestparser.getcontentfrombody (httprequest) function. Here, you can directly detect whether the form data is a JSON string. If yes, add an attribute: "state" as the return value, otherwise you will return a JSON string containing only the attribute "state"

Here, the post operation is performed on the postman tool

The above three examples are called on the computer, which has the same effect as calling on the mobile phone

That's all for the basic operations. For more details, see the example code: Android server

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
分享
二维码
< <上一篇
下一篇>>