Android Tutorial Embed an Http Server in your Android Application

Status
Not open for further replies.
The HttpServer library is a new library, based on an open source project named Jetty. This library allows you to easily embed an Http server in your application.

Http server means that you can point a browser to the device IP address (and relevant port) and communicate with your app.

It can be very useful in many applications.

Some of the benefits or reasons to use this library:
- No need to write any additional "client" app as the server can be accessed from any browser
- Much simpler to manage than raw sockets
- As the device plays the server role there are usually no firewall issues (when working in the local network)

The basics of this library are quite simple, though if you are not familiar with the Http protocol then it is recommended to first get familiarized with it.

How it works?
First you should initialize the server and call start with the chosen port. You should (usually) use a Service to handle the server.

Once the server is running, any external request will fire the HandleResponse event.

For example to create an interesting site that always returns a simple string you can write the following code:
B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
   Response.SendString("Hello")
End Sub

You can get more information from the Request object and then set the response accordingly.

The attached example demonstrates the features of this library.

Main page:

SS-2013-02-03_17.45.10.png


The code that builds this Html response, reads a template file and then replaces some of the fields as needed.
Note that the Greeting value is set in the main Activity.

The browser URL is made of the device local IP address and the chosen port.

This is the template text:

SS-2013-02-03_17.48.09.png


Pressing on the "List external storage files" link will take you to the "list" page:

SS-2013-02-03_17.50.25.png


The list page allows you to download or view the files, navigate to a different folder or to upload a file. Uploading a file is done with an "upload" call.

The server supports GET and POST requests. It will automatically handle multipart/form-data post requests (like is done with the uploading form).

The possibilities with this library are endless! You can pull reports from the device, export or import data, remotely control the device, handle multiple clients and many other options.

The library, which is quite large due to the Jetty library (1.5mb), can be downloaded here:
www.b4x.com/android/files/HttpServer.zip

The example is attached.

Other examples:
Multi-chat: http://www.b4x.com/forum/basic4android-getting-started-tutorials/25984-embed-http-server-your-android-application.html#post150462
Database reports: http://www.b4x.com/forum/basic4android-getting-started-tutorials/25984-embed-http-server-your-android-application-2.html#post150723

A module that allows you to use No-IP.org service and access devices over the internet: http://www.b4x.com/forum/basic4andr...ess-your-device-over-internet.html#post153944
 

Attachments

  • HttpServerExample.zip
    58.1 KB · Views: 7,538
Last edited:

thoscho2003

Member
Licensed User
Longtime User
hi Erel

I work with a mobile phone with android 4.0.3, on which is established a mobile hotspot. when I start your server example I get only "Device not connected to local network". In the log appears the ip 127.0.0.1 and the following messages:

** Activity (main) Create, isFirst = true **
Ip address: 127.0.0.1
** Activity (main) Resume **
** Service (serverservice) Create **
2013-12-11 11:01:20.527:INFO::jetty-7.x.y-SNAPSHOT
2013-12-11 11:01:20.641:INFO::started o.e.j.s.ServletContextHandler{/,null}
2013-12-11 11:01:20.648:INFO::Started BlockingChannelConnector@0.0.0.0:5555 STARTING
org.eclipse.jetty.server.Server@416ace60
** Service (serverservice) Start **

what can I do to connect my server to the hotspot and to retrieve the ip-adress?

thanx in advance
 

AndroidUserCCS

Member
Licensed User
Longtime User
Hi Erel Beautiful code and example, I deploy it in my blu dash 4.0 and using your noip library too.

In my router I use a port forwarding from 80 to 8080 (my router use dd-wrt firmware)

Android cannot open the port 80 so I rooted my phone, use Port Forwarded Ultimate I get it from google play store and works!

You can see this in http://pm.zapto.org is the database example posted by you. (is my cellphone connected to wifi lan so can be slow.)

Edit: Now is running under my samsung note 2, if wifi is not available is run under mobile connection. (I want to know if my provider give me access)
 
Last edited:

juanramondroid

New Member
Licensed User
Longtime User
Hi Erel. I'm one question please. There is possible to connect without WIFI, throught the mobile data connection?
 

thoscho2003

Member
Licensed User
Longtime User
The use of the html template is nice to transfer actual status informations. But is there a similar possibility to include a live/preview image into the webpage? Maybe the image is cached in the RAM.
 

thoscho2003

Member
Licensed User
Longtime User
thanx

is it possible to include "prev.jpg" as such a temporary immage file with a html tag in that way:

src= "DirInternalCache/prev.jpg" ?
 

pauleffect

Member
Licensed User
Longtime User
@Erel , the chat app seems very interesting. I was wondering if you could help me get the user name from the service into a variable in Main. I think it's done with callsub (though the exact implementation eludes me).
Would you kindly shed some light on this matter?
Thanks.
PS: i sincerely apologize for my stupid questions. i have an awkward condition that renders docs (mostly) useless to me. so I'm learning by following examples and forum posts. Thanks again!
 

pauleffect

Member
Licensed User
Longtime User
On it!
Thank you!
_________
I thought public var's were declared once and used "as is" throughout the program. This is the first time I bumped into globals and noticed how you used them. I sincerely believe these little pieces of code are 1000x more useful than all the docs anyone could possibly write for b4a combined.
 
Last edited:

EvgenyB4A

Active Member
Licensed User
Longtime User
Hello,
Is it possible that HTTP Server will work at Ethernet network instead of Wifi?
 

DonManfred

Expert
Licensed User
Longtime User
Hello,
Is it possible that HTTP Server will work at Ethernet network instead of Wifi?

If your device is connected to your LAN with LAN-Cable then you can use lan, sure. But your device don´t have an ethernet-plug i suppose... So your device is connected to your lan ONLY over wifi
 

EvgenyB4A

Active Member
Licensed User
Longtime User
If your device is connected to your LAN with LAN-Cable then you can use lan, sure. But your device don´t have an ethernet-plug i suppose... So your device is connected to your lan ONLY over wifi
My device is connected to Lan via Ethernet and the application says that there is no connection. When I use Wifi that there is connection an app shows the IP address. I saw in one of Erel replyes that this app works only with WiFi, but I need that it will work with Ethernet too.
 
Status
Not open for further replies.
Top