How to host REST web services on Android?

Does anyone know an open source web server written in Java capable of hosting REST services on Android?

I tend to https://github.com/NanoHttpd/nanohttpd , but for this you will need to create a web services structure from scratch.

ps. Websockets will be a plus. ps2. I am looking for a hosting solution, not a client and the device is quite powerful.

+6
source share
3 answers

Before a similar problem, we decided to go with Koush AndroidAsync:

https://github.com/koush/AndroidAsync

This is a fairly convenient project for a number of reasons. In addition to supporting websockets, you can implement a basic POST or GET handler in a few lines and pass JSON around which is all we need. Example from this github page (supports POST in the same way):

AsyncHttpServer server = new AsyncHttpServer(); List<WebSocket> _sockets = new ArrayList<WebSocket>(); server.get("/", new HttpServerRequestCallback() { @Override public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { response.send("Hello!!!"); } }); // listen on port 5000 server.listen(5000); // browsing http://localhost:5000 will return Hello!!! 
+10
source

There are many third-party HTTP servers for Android. Also, since the Android Level 1 API has an HttpService: http://developer.android.com/reference/org/apache/http/protocol/HttpService.html , built-in.

Any HTTP server is capable of REST.

If you mean that you need a server-side REST infrastructure for Android (ala JAX-RS or Restlet or one), then this is a different perspective, and I am not familiar with the one that will work out of the box on Android (although they, probably really is). The reason for this is because you usually do not run the HTTP server on your mobile device. Yes, you can, and at certain times it’s convenient, but it’s just not a general requirement.

As for websockets, this is a completely different issue and has nothing to do with REST (so what does this confuse?). There are also many Android apps (koush's, for example: https://github.com/koush/android-websockets ).

+6
source

If you intend to host your web services from an Android device. This is an unusual case, but in the restlet structure there is a server component that runs on android. Checkout http://restlet.org/learn/guide/2.2/editions/android/ .

It does not require a separate web server and is rather meager. I have not tried it as an android server personally, so I can not vouch for how well it works there, but I used it autonomously and on appengine.

You need to get away with something like code wrapping in an Android service

 Component serverComponent = new Component(); serverComponent.getServers().add(Protocol.HTTP, 8080); final Router router = new Router(serverComponent.getContext().createChildContext()); router.attach("/myrestendpoint", MyServerResource.class); serverComponent.getDefaultHost().attach(router); serverComponent.start(); 
+3
source

Source: https://habr.com/ru/post/947265/


All Articles