Android - Create RESTful WebService - Host it on mobile - android

My question is how to create a web service on android device & host it on the mobile device.
I don't want to access an already created webservice deployed on a server; I want to create a webservice that returns json/xml & host it on mobile.
RESTful Web Services Implementations in Mobile Devices says it's doable for Java-based mobile devices, so I think it can be done for Android too.

There is one problem with your setup and mobile devices: mobile networks (wifi and 3g) mostly use NAT, which prevents inbound connections. This makes mobile devices inaccessible from the internet (inbound) while they can still initiate outbound connections. For this reason mobile devices mostly do not host servers.

What you can do is make an Android service which pings a server ( a web-server) every 30 minutes or so simply send a bit from client to server and back to check the availability of request from another phone. This way you can upload media files to server and download them to your another device.

I am trying to solve the same problem and I am leaning towards https://github.com/NanoHttpd/nanohttpd.
It is a tiny webserver without any REST or MVC functionality, thus you would have to write it yourself (MVC, content negotiation, etc).

Related

HTTP\WebSocket server under Cordova application

I've faced an issue with creating any kind of servers under Cordova application. Wondering if it possible at all.
Scenario
Let's imagine that we have a decentralized network of devices (tablets, phones) from one tablet to a huge network of 20+ devices and can be more. These devices must handle synchronization between each other and it can be implemented as HTTP server or WebSocket server, etc...
Each device have installed Cordova application and our goal is to make these devices discoverable in a network (I was thinking about UDP datagrams for discovery) and these devices must synchronize data between them (simple TCP connections and JSON objects).
Question
For this scenario we need to have an access to a network layer in Cordova application. So, our Cordova application when running onto device can be as server and as client as well. The question here is how to implement UDP\TCP server in Cordova application, since we have only client implementations?
P.S. I was trying plugins like cordova-plugin-chrome-apps-udp, cordova-plugin-websocket and cordova-plugin-websocket-server but it still doesn't work properly (I'd like to say it's not working at all)

Access REST end point on an embedded device using WiFi direct from android

I have an embedded device that exposes its functionalities via a RESTful Web service. I want to consume those Web services via my android device by connecting my mobile to the embedded device via Wifi? Is it possible to do so?
The possible use cases are transferring the control panel UI of the embedded device to the android and controlling it remotely. The UI is exposed as a REST endpoint.
If the chip you are using supports Wi-Fi Direct, you should search for its code yourself since I don't know about it really, but you can find documentation and examples of wifi direct here, and if it doesn't support it, there are two scenarios, one is that you want to make your android device a server and your chip a client, which leads you to use Nanohttpd in your android code to make a server, and the other one is to bring up a server on your chip and make your android device a client, which leads to using a library like loopj which saves lots of pain. so I guess that was all.

Host webservice on Android

I've only found tutorials to consume a web service with Android.
But what I want to do, is to host a web service on a Android phone and consume this web service with a .Net application.
Does someone know a good tutorial or something like this?
You could use i-jetty, a webserver for android.
Also there are some resources for your question:
RESTful Web Services Implementations in Mobile Devices
Android - Create RESTful WebService - Host it on mobile
However, according to this answer:
...mobile networks (wifi and 3g) mostly use NAT, which prevents inbound connections. This makes mobile devices inaccessible from the internet (inbound) while they can still initiate outbound connections. For this reason mobile devices mostly do not host servers.
It could also drain your device's battery.
Hope this clarifies your doubts.

Web server on Android with remote access possibility

can someone confirm, that it is possible to create a web server on android device ( which uses 3G or 4G internet ) and access it via internet ( from the remote PC connected to the internet, not from the same local network ).
I know there already are some web servers for android ( iJetty, nanohttpd ) and the programms like ksweb, so it seems, that it should be possible.
However, I've done some searching in the internet and some people warn, that mobile devices work using NAT protocol, so it would be impossible.
Can somebody resolve the ambiguity?
Thank you in advance.
No, it is generally not possible. The dealbreaker problem is not with Android, but rather that just about any mobile network provider is going to block incoming connections.
For special purposes, you can do things like have the phone contact a publicly visible remote machine and create a tunnel with something like ssh through which inbound connections can then be sent if there is a server program listening on the loopback interface of the Android device. But this tends to be only useful for things like remote testing.
Were you using wifi you could do this, but you'd need to accept that Android is not designed to promise a high availability of any 3rd-party program. You would need a wakelock to keep the processor running. And you may even need to do things like periodically initiate outbound traffic from the device in order to keep the wifi stack/radio in a fully active mode so that it can accept connections.
Generally speaking, data you want to make remotely available from the device should be proactively uploaded to a mutually visible server. If you want to send requests to the device, look at using something like Google Cloud Messaging which is designed to wake up the device and a recipient application on it in order to deliver a brief message.

(Android, iOS, Windows, Linux) Server Polling Vs Push vs Implement Server

I'm building a multi-OS mirroring system which I would like to implement using a hybrid client-server and p2p communication method (at least that's the best way I have of describing it).
My issue is that at some point I have a central server (appengine, so there are limitations to what I could do because of time and networking capability constraints) that would need to get a message to a host of different devices which are not necessarily running the same OS (Windows, Android, iOS, Linux, etc...).
Android and iOS (or any other mobile platform) are the main problems it looks like I will be having on 2 levels.
1 - They are both limited by battery power (more so than a laptop and desktops shouldn't have that issue at all), so whichever method I use needs to take that into account.
2 - NAT (harder because the user has relatively less control over their firewall than on a network that they are running). My central server will maintain a table of which device has what IP address, but from what I understand if there is NAT or a firewall it won't be able to get to it if the port was not forwarded.
Since I will be writing a specific client for each OS I prefer a solution that is more universal. I have been leaning towards writing an extremely simple HTTP server that sits on each client and takes requests (which appengine is able to send) and treats them as messages that alert the client to perform an action (either with the server or another client). However, I run into the issue of NAT/firewall. For instance if appengine needs to send a message to AndroidDevice1 it would grab its IP address from a table and make a request to it. However this doesn't work if the ports aren't forwarded correctly, and if the user is on 3g/4g the firewall is controlled by the data provider.
Because of this, I started thinking about using Android C2DM but I want a solution I could implement across platforms.
The only other method I could think of is to just have the client poll the server for messages. This has the battery and network consumption issue though.
Would there be any other way to implement this, and if not, which one of the above methods are best in terms of balancing usability, power and data consumption and user input (the less the user has to do to get the client set up (ie port forwarding, etc...) the better)? Please note that I do not intend for this to become a discussion/flame war but a logical presentation of facts.
Thanks in advance!
You can create a persistent TCP connection from the device to the server and then communicate over this open connection. This would be a very simple connection with keepalive packets for the most part.
In theory this would consume some battery through the radio, but in practice I have experienced that the battery is not affected much at all. Key is to keep the communication over this line to a minimum.
If AppEngine does not allow this approach, you can run your own socket server and then communicate between this server and the appengine server using REST. A socket server I have used is Apache MINA and had no issues with scalability.
Another problem you will have with this approach or any other approach is that on iOS (afaik) you cannot keep a tcp socket open when the App goes into background. The only thing to communicate with the iOS device is Apple Push Notification Service
I would prefer rather than having HTTP Connection you should create TCP/IP tunnel and make communication fast and reliable. I have one Chat application which runs perfact for me using TCP/IP. If you use this you will have same logic for multiple platforms. Only thing you need to write is different code for iOS and android.

Categories

Resources