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:

lorebarita

Member
Licensed User
Longtime User
php with httpserver

Hi is it possible to have php running with this http server library?
Thanks
Lorenzo
 

javiman6969

Member
Licensed User
Longtime User
And insert data into a table

an example of how to insert data into the database?

Another example: HttpServer Database
This example is based on DBUtils. It demonstrates how HttpServer library can be used to access device reports.

The browser sends a query to the server which then returns an Html table (by calling DBUtils.ExecuteHtml):

SS-2013-02-05_16.47.18.png
 

augustin.thierry.sin

New Member
Licensed User
Longtime User
Hello,
Thanks you for this library !
But I've got a problem with it, i don't know how I can display a variable from the main program in the web server. :sign0085:
 

augustin.thierry.sin

New Member
Licensed User
Longtime User
Hello,
I do not want to make a text field, I have a random variable with a timer (to change the value) and I need to display it into the website. How I should do it ?
Thanks.
 

dune3000

Member
Licensed User
Longtime User
Hi, Erel

Thanks for your great lib!
I want to make a simple REST server.
There is a problem, I use Firefox to test the server, but I don't know how to get the Body Message.

attachment.php


How can I do that?
 

Attachments

  • RESTsend.jpg
    RESTsend.jpg
    27.1 KB · Views: 1,600

Erel

B4X founder
Staff member
Licensed User
Longtime User
Currently HttpServer supports two types of requests. GET requests where the parameters are send in the URL and multipart/form-data POST requests (as send from all the standard html Forms). These are the most common types of requests that can be sent from a browser. I guess that you are using a Filefox plug-in for this POST request.
 

dune3000

Member
Licensed User
Longtime User
You are right, Erel. I use the REST Client plugin to test my server, because my REST client(on other device) uses the same method to send the request.
Now I need all your HTTP server's function, if I can touch the original data of "multipart/form-data", it will be perfect for me!

Thanks.
 

bernd33

New Member
Licensed User
Longtime User
Version?

Hi,

wich version of Jetty is this?

Dose Websockets work? If yes, has somebody already used this?

The reason why I ask: I have some Atmega Microcontrollers with network interface. This uC are controlled by a Android mobile phone (communication with UDP). Works realy well.

With a HttpServer it's possible to controll the Microcontollers from any device (Windows, Apple, Android). Great. But, to see actual values from the Microcontollers I have alway to reload the page.

Maybe, it's possible with Websockets to make a "realtime view/control" Webpage for the Microcontrollers?

Comments would be great...

Regards, Bernd
 
Status
Not open for further replies.
Top