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:

Douglas Farias

Expert
Licensed User
Longtime User
@Erel how to upload multiple files on this example?
on the html is only add <multiple> at form, you can select multiple files, later u see 3 files selected, but when you go to upload, press a button to upload, this upload only one. how to make a multiple upload to device?
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi Erel.
my note 3 device get force close error when i enter ip in addressbar why?
 

IslamQabel

Active Member
Licensed User
Longtime User
If i want to display a text of a label found in the activity module(e.g. LabelA) such that i want to display it in the web page when i access the server from any PC web browser.
can i use the following code:
B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
   Response.SendString(LabelA.text)
End Sub

noting that ... i built html page using general html editor not by B4J
thanks
 

IslamQabel

Active Member
Licensed User
Longtime User
i know that the server code should be in service module....I am asking how to display the text of LabelA which found activity module to be displayed in the web page
 

IslamQabel

Active Member
Licensed User
Longtime User
i want to build more than one web page .....for example three web pages so the user can move from one to the others how to do that?
Thanks
 

IslamQabel

Active Member
Licensed User
Longtime User
In the first example, i see "list" page.....so if i want to add for example "page2.html" should i add the same subroutines like the "main page" in the service module?
 

IslamQabel

Active Member
Licensed User
Longtime User
Hi Erel....
in the service module.....if i add the following in sub Server-HandleRequest
B4X:
Case Request.RequestURI,StartWith("/page2/")
         HandlePage2(Request,Response)
and adding this new sub-routine:
B4X:
Sub HandlePage2(Response As ServletResponse)
      Dim Page2 As String=GetTemplate("Page2.html")
     '******displaying the time in the second page***
     Page2=Page2.Replace("$TIME$",DateTime.Time(DateTime.Now))
Response.SetContentType("text/html")
Response.SendString(Second)
End Sub
are these additional codes are correct?
 
Status
Not open for further replies.
Top