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:
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.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
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.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
Last edited: