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,309
Last edited:

DonManfred

Expert
Licensed User
Longtime User
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.

May i ask what device you have which have an ehternet-port?
 

EvgenyB4A

Active Member
Licensed User
Longtime User
The device I use has both ethernet and wifi ports. I think the problem is the same as in B4A-bridge app. The B4A-bridge also works with Wifi only. So I need that HTTP server and maybe B4A-bridge could work with ethernet and maybe with 3G.
 

EvgenyB4A

Active Member
Licensed User
Longtime User
I think I have solved the issue by changing ssocket.GetMyWifiIP to ssocket.GetMyIP in HTTPServerExample. Now it works on Ethernet too.
 

EvgenyB4A

Active Member
Licensed User
Longtime User
If you know the device ip address then you can connect to it with any valid connection. What I meant to say without a more complex solution it is not possible to access the server over the internet.
I use internal network for a while. It needs to configure firewall to access the server from outside.
 

IslamQabel

Active Member
Licensed User
Longtime User
Dear All:

I am Asking about The web page in Firefox, how to build it? or it is already built by the Embedded Http server in my mobile???? i am somehow confused
 

IslamQabel

Active Member
Licensed User
Longtime User
Very good Thanks Erel

Can i make some changes in graphics and font of the built page or it is unavailable ?
 

DonManfred

Expert
Licensed User
Longtime User
Have a look at the source of this example (Files/main_template.html)...
<html>
<body>
<img style="width:200px;height:200px;" src="android.png">
<p>
This page is served from an Android device!<br/>
The time here is: $TIME$<br/>
Greeting: $GREETING$<br/>
Other information:<br/>
$OTHERINFORMATION$
</p>
<a href="/list/">List external storage files</a>
</body>
</html>
 

pauleffect

Member
Licensed User
Longtime User
Very good Thanks Erel

Can i make some changes in graphics and font of the built page or it is unavailable ?

You can specify whatever html file you want. I didn't try an external css(it should work though), but you can embed css rules in the html file of your choice.

If you place certain keywords in the html file, you can replace those with variables generated by your app.

Just treat the situation like it would be a classic web design thing and that's it.
 

IslamQabel

Active Member
Licensed User
Longtime User
Thanks all.....
I will try it but unfortunately i do not know well about CSS rules and also web design rules but i will try to make different web page....If you have a link that could help me that is will be OK......................I do not want to learn new language because i am programming with C for micro-controllers and B4A so i can not add another language
 

IslamQabel

Active Member
Licensed User
Longtime User
So is there is a library in B4A to design web page as seen in (Files/main_template.html)? as DonManfred told me
 

IslamQabel

Active Member
Licensed User
Longtime User
Dear All:

Erel Said:
"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."


How to access the server as i can not access it via the Internet? what is the link between PC and the mobile phone?
 
Status
Not open for further replies.
Top