Android Tutorial Embed an Http Server in your Android Application

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
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:



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:



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



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.basic4ppc.com/android/files/HttpServer.zip

The example is attached.

Other examples:
Multi-chat: http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/25984-embed-http-server-your-android-application.html#post150462
Database reports: http://www.basic4ppc.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.basic4ppc.com/forum/basi...ess-your-device-over-internet.html#post153944
 

Attachments

Last edited:

wl

Well-Known Member
Licensed User
ny

Great !

Anyone an idea how many mobile carriers allow incomming port 80 requests to their mobile subscriptions ?
 

barx

Well-Known Member
Licensed User
A full bag of awesomeness....

yeee haa
 

mc73

Well-Known Member
Licensed User
Excellent! I'm wondering if the connection speed is similar to native sockets handling.
 

Erel

Administrator
Staff member
Licensed User
Here is a quick example of a multi-chat solution. It was written in less than an hour (though it is not perfect...).

Any number of clients / browsers can connect to the server and join the chat. A simple Javascript updates the messages every 5 seconds so other users can see new messages.

Screenshots:





A browser tool such as Firebug is really useful in order to debug such solutions.
 

Attachments

mc73

Well-Known Member
Licensed User
It depends. File transfers are slower as files are encoded in base64 (according to the multipart protocol).
I understand. Actually I was wondering whether this great lib could fit my multi-socket app. I am happy with connection speeds, working with sockets, yet if the delay is no more than 200-300ms for connection, I would probably be on my way to integrating this lib in a future version!
 

monki

Active Member
Licensed User
Hi Erel,
Thank you for this great lib.

but unfortunately does not file upload.
I have inserted the following log commands,
B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
Log(Request.RequestURI)
Log(Request.Method)
but it is after the upload buttons press no request logged in the sub Server_HandleRequest.
tested with Firefox and Internet Explorer

monki
 

monki

Active Member
Licensed User
@ Erel
Erel you're right, I tested it with a file and works great 5Kb esa.
I was too impatient.
Thank you very much

monki
 
Last edited:

derez

Expert
Licensed User
erel
Thank you for this great library and application.
So simple to use, so many possible uses.
 
Status
Not open for further replies.
Top