Android Tutorial Embed an Http Server in your Android Application

Discussion in 'Tutorials & Examples' started by Erel, Feb 3, 2013.

  1. Erel

    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:
    Code:
    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:

    [​IMG]

    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:

    [​IMG]

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

    [​IMG]

    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
     

    Attached Files:

    Last edited: Feb 23, 2014
    AnandGupta, Beja, carlos7000 and 15 others like this.
  2. sktanmoy

    sktanmoy Active Member Licensed User

    Wow, great :)
     
  3. wl

    wl Well-Known Member Licensed User

    ny

    Great !

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

    barx Well-Known Member Licensed User

    A full bag of awesomeness....

    yeee haa
     
  5. JTmartins

    JTmartins Active Member Licensed User

    :)

    OMG...

    Jose
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    It seems like most mobile networks block all incoming connections. Also note that you can only open ports larger than 1024 (lower ports can only be opened by system processes).
     
  7. sioconcept

    sioconcept Active Member Licensed User

    Excellent ! We can use it to add more content.

    Thanks !
     
  8. mc73

    mc73 Well-Known Member Licensed User

    Excellent! I'm wondering if the connection speed is similar to native sockets handling.
     
  9. Erel

    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:

    [​IMG]

    [​IMG]

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

    Attached Files:

  10. Erel

    Erel Administrator Staff Member Licensed User

    It depends. File transfers are slower as files are encoded in base64 (according to the multipart protocol).
     
  11. mc73

    mc73 Well-Known Member Licensed User

    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!
     
  12. monki

    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,
    Code:
    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
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Are you sending a large file? It may take some time till the event is fired.
     
  14. mc73

    mc73 Well-Known Member Licensed User

    No. Just some bytes.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    The question was actually intended for monki...
     
  16. mc73

    mc73 Well-Known Member Licensed User

    I noticed it afterwards :D
     
  17. 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: Feb 4, 2013
  18. derez

    derez Expert Licensed User

    erel
    Thank you for this great library and application.
    So simple to use, so many possible uses.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    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):

    [​IMG]
     

    Attached Files:

  20. avacondios

    avacondios Active Member Licensed User

    Hi Erel,

    Does it supports Json response rather than to return html?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice