In a recent project, because Android is used as a server to perform a real-time data reception function, it is necessary to make an Android local micro server. At this time, I first thought of spring boot, because it is a server framework. But in fact, we don't need such a large server framework at all, and it's too troublesome to configure them. So, I found three frameworks: Ijetty, NanoHttpd and AndroidAsync, which are relatively small and suitable for Android. After comparison, Ijetty is too complicated to use, and it will inexplicably report some problems that are not easy to solve, so it was abandoned. Since I didn't study Ijetty in detail, I focused on NanoHttpd and AndroidAsync; So let's first talk about the advantages and disadvantages of the two: 1. NanoHttpd is a framework with BIO as the underlying encapsulation, while AndroidAsync is encapsulated with NIO as the underlying encapsulation. The rest is the same, and in fact AndroidAsync is written based on the NanoHttpd framework. So, in a sense, AndroidAsync is an optimized version of NanoHttpd, but of course it also depends on the specific application scenario. 2. NanoHttpd can only be used for HttpServer, but AndroidAsync can be used in webSocket, HttpClient, etc. in addition to HttpServer applications. Among them, the Ion library separated from AndroidAsync is also relatively famous. 3.NanoHttpd's underlying processing includes many return status codes (for example: 200, 300, 400, 500, etc.); but after reading the source code of AndroidAsync, I found that there are only two status codes returned by the underlying encapsulation of AndroidAsync: 200 and 404. I just found this pit (the example of OPTIONS will be mentioned below) Let’s take a look at the specific usage below. 1. Let’s talk about NanoHttpd first: Because the NanoHttpd framework is actually a single file, you can download it directly from GitHub. Download address With the downloaded file, you can inherit this file and write a class as follows:
Based on the above example, the following points are mainly made: 1) All requests can be received, whether it is post or get, or other requests. If filtering is required, handle it yourself; 2) Note that the problem of not receiving post parameters handled above has been given a reference link in the code comments, please refer to it; 3) If the request contains both an interface and a static resource (such as HTML), then be careful to distinguish the two requests, for example, you can use URI to identify them; of course, both can be returned in the form of a stream, and both can call the API method newFixedLengthResponse(); 4) The author suggests that you should first deal with the cross-domain issue, because Android may be debugged with h5, so it is easier to debug after setting up the cross-domain. Of course, some scenarios can be ignored, depending on personal needs; the method has been written in the above code; 5) Of course, the most important thing is the opening and closing code:
2 Let’s take a look at AndroidAsync: This framework is quite interesting and has many functions. This article will only talk about the relevant knowledge of HttpServer and leave the rest for later reference. As usual, let's talk about usage first: Add in Gradle:
Code example: (Cross-domain is not handled here. If necessary, please handle it according to the previous example)
Based on the above example, the following points are mainly mentioned: {It is probably about the usage of API} 1) For example: server.addAction("OPTIONS", "[\d\D]", this) is a general method for filtering requests. The first parameter is the request method, such as "OPTIONS", "DELETE", "POST", "GET", etc. (note that they are in uppercase), the second parameter is the regular expression for filtering URIs, here all URIs are filtered, and the third is the callback parameter. server.post("[\d\D]", this), server.get("[\d\D]*", this) are special cases of the previous method. server.listen(PORT_LISTEN_DEFALT) is the listening port; 2) request.getHeaders().getMultiMap() This is where you get the header parameters, so be sure to keep it in mind; 3) ((AsyncHttpRequestBody<Multimap>)request.getBody()).get() is where the parameters of the post request are obtained; 4) The code for obtaining static resources is in the else of the callback method onResponse, as shown in the example above. 5) Let me talk about the pitfalls of OPTIONS. Because there are only two kinds of http status codes encapsulated in the AndroidAsync framework, if the filtering method does not include a request method such as OPTIONS, the actual http status code returned to the client is 400, and the error message reflected in the browser is actually a cross-domain problem. It took me a long time to find it, so please pay attention. Summarize: 1) The same page: NanoHttpd time: 1.4s AndroidAsync time: 1.4s However, when entering for the second time, the time consumed by AndroidAsync was obviously less than the first time. The author speculates that this is because AndroidAsync does some processing at the bottom layer. 2) From the perspective of API analysis, NanoHttpd is more convenient to use, and the APIs for obtaining the passed parameters are easier to use; AndroidAsync's API is relatively more complicated, such as obtaining params. 3) If you analyze it from the perspective of the scenario, if you need high concurrency, you must use AndroidAsync; but if you don’t need it, then analyze it in detail. |
>>: Discussion on optimization scheme for opening the first screen of mobile H5 within seconds
Whenever we take stock of IP cooperation cases, H...
[51CTO.com original article] Memcached is a free,...
Bilibili is a product that is very popular among ...
This may be the most comprehensive beauty and ski...
In the first half of 2019, mobile tool apps ranke...
Recently, a new way of raising fish has become po...
Everyone has the same purpose for making money th...
Faced with high public domain customer acquisitio...
If you want to do your work well, you must first ...
How much does it cost to be an agent for a moving...
More and more businesses are paying attention to ...
8,000 kilometers away from China and with a 5-hou...
Among all the factors that affect a company's...
Hello everyone I am a drop of lovely (no objectio...
It has been almost half a month since the Mid-Aut...