Sample code for implementing httpserver on Android

In the recent project, because we want to use android as a server to do a function of receiving data in real time, we need to do a local microserver for Android at this time.

At this time, I first thought of spring boot, because it is a server framework. But in fact, we can't use such a large server framework at all. It's too troublesome to configure these. Therefore, I found three frameworks, ijetti, nanohttpd and androidasync, which are relatively miniature and suitable for Android.

After comparison, ijetti is too complex to use, and it will report some problems that are not easy to solve inexplicably, so it is abandoned.

Because I didn't study ijetti carefully, I focused on nanohttpd and Android async; Let's start with the advantages and disadvantages of the following two:

1. Nanohttpd is a framework with bio as the underlying package, while androidasync is encapsulated with NiO as the underlying package. The others are the same. In fact, androidasync is written following the nanohttpd framework. Therefore, in a sense, Android async is an optimized version of nanohttpd. Of course, it also depends on the specific application scenario.

2. Nanohttpd can only be used for httpserver, but androidasync can also be used in websocket, httpclient, etc. in addition to httpserver, ion library separated from androidasync is also famous.

3. The bottom processing of nanohttpd contains many return status codes (such as 200, 300, 400, 500, etc.); However, after reading the source code of androidasync, the author found that there are only two status codes returned by the underlying package of androidasync: 200 and 404. The author just found this pit (I will talk about the example of options below)

Let's take a look at the specific usage.

1. Let's start with nanohttpd:

Because the nanohttpd framework is actually a single file, which can be downloaded directly from GitHub

With the downloaded file, you can inherit the file and write a class, as follows:

According to the above examples, the following points are mainly mentioned:

1) Requests can be received, whether post or get, or other requests. If you need to filter them, you can handle them yourself;

2) Note that the problem of not receiving the post parameter handled above has been given a reference link in the code comments, please refer to it;

3) If there are both interfaces and static resources (such as HTML) in the request, pay attention to distinguish the two requests. For example, you can use URI to identify them; Of course, the return can be in the form of flow, and the API method newfixedlengthresponse() can be called;

4) The author suggests that it's best to deal with the cross domain problem, because Android may be jointly debugged with H5, so it's more convenient to debug after setting up cross domain. Of course, some scenarios can also be ignored, depending on personal needs; Method has been written in the above code;

5) Of course, the last and most important point must be the code for opening and closing:

2 take another look at androidasync:

This framework is more interesting and has many functions. This paper directly talks about the relevant knowledge of httpserver. The rest are not shown in the table below.

Old rule, first use:

Add to gradle:

Code example: (cross domain is not handled here. If necessary, please handle it according to the previous example)

According to the above examples, the main points are as follows: {it is about API usage}

1) For example, server. Addaction ("options", "[\ D \ D]", this) is a general method for filtering requests. The first parameter is the method of the request, such as "options", "delete", "post", "get", etc. (note that it is capitalized). The second parameter is the regular expression for filtering URIs. Here is to filter all URIs, and the third is the callback parameter. Server. Post ("[\ D \ D]", this) and server. Get ("[\ D \ D] *", this) are special cases of the previous method. Server. Listen (port_listen_default) is the listening port;

2) Request. Getheaders(). Getmultimap() is the place to get the header parameter. You must keep it in mind;

3) ((asynchttprequestbody < Multimap >) request. Getbody()). Get() is the place to get the parameters of the post request;

4) The code for obtaining static resources is in the else of the callback method onresponse, as shown in the example above.

5) Let's talk about the pitfalls of options, because there are only two status codes for returning HTTP encapsulated in the androidasync framework. If the filtering method does not include the request method of options, in fact, the HTTP status code returned to the client is 400, and the error information reflected in the browser is a cross domain problem. It took a long time to find it, please note.

Summary:

1) Same page:

Nanohttpd time: 1.4s

Androidasync time: 1.4s

But the second time I went in, Android async took significantly less time than the first one. I guess it's because the underlying Android async did some processing.

2) From the API analysis, the usage of nanohttpd is more convenient, and those APIs that obtain the passed parameters are easier to use; The API of Android async is relatively complex, such as the acquisition of params.

3) For scenario analysis, if you need high concurrency, you must use Android async; But if you don't need it, analyze it in detail.

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