Android Tutorial Embed an Http Server in your Android Application

Status
Not open for further replies.

mlc

Active Member
Licensed User
Longtime User
Hello,

It's possible to upload files with Ajax?

Thanks
 

IslamQabel

Active Member
Licensed User
Longtime User
Hi Erel:

No it did not work as i want so i am asking you, However i tried this code and i want to know what is the wrong with it:
B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    Try
        Log("Client: " & Request.RemoteAddress)
        Log(Request.RequestURI)
        Select True
            Case Request.RequestURI = "/"
                HandleMainPage (Response)
            Case Request.RequestURI.StartsWith("/Page2/")
              HandlePage20(Response)
    End Select
    Catch
        Response.Status = 500
    End Try

End Sub

then i added a new sub-routines to handle "Page2.html" which is saved in Files folder of my project:
B4X:
Sub HandlePage20(Response As ServletResponse)
      Dim Page20 As String=GetTemplate("Page2.html")
     '******displaying the time in the second page***
     Page20=Page20.Replace("$T$",DateTime.Time(DateTime.Now))
Response.SetContentType("text/html")
Response.SendString(Page20)
End Sub

although the compiler did not display any error message, when i type in web browser 192.168.1.100:5555/Page2 or /Page2/ or /Page2.html i saw blank page

thanks
 
Last edited:

IslamQabel

Active Member
Licensed User
Longtime User
how to add login filter to the main page? username & password are displayed when i access to the server ?
 

mlc

Active Member
Licensed User
Longtime User
It's possible to upload files with Ajax?

The server expects files encoded in the multipart protocol. I don't think that it will work with ajax uploads.

Hello,
With this code, I can upload multiple files to the server.

B4X:
<!DOCTYPE html>
<html>
<head>
    <title>XHR1</title>
    <script>

    var xhr = new XMLHttpRequest();

    function send(){

      var fd= new FormData(document.forms.namedItem("fileinfo"));
      xhr.open("POST", "/uploadf/", true);
      xhr.send(fd);
    }

    xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert(xhr.statusText);
                    }
              }

    </script>
</head>

<body>
<form enctype="multipart/form-data" method="post" name="fileinfo" action="/uploadf/">
  <input type="file" id="file-select" name="file" required/ multiple>
  <input type="submit" value="Send Files" onclick="send" />
</form>

</body>
</html>

In the image below, you can view the files in the temporary folder and the file names in the request (the value is an array).
Key = "file"



But the problem is that Request.GetUploadedFile("file") expects a string.

B4X:
Sub HandleUploadf(Request As ServletRequest, response As ServletResponse)
    Dim upload As String = Request.GetUploadedFile("file")
    'copy the temporary file to the correct folder
    File.Copy(server.TempFolder, upload, Main.Recibidos, Request.GetParameter("file")) 'the file name is sent as a parameter
    File.Delete(server.TempFolder, upload) 'delete the temporary file.
    response.SendString("OK")
End Sub

and do not know how to extract individual files.

Any ideas? It is possible?

Note: The code works fine for a single file.

thanks
 
Last edited:

acbarbosa

New Member
Licensed User
Longtime User
Hello Erel,

Is it possible to use this HTTP server with a HTTPClient using WebView or WebViewExtras, or something similar inside the same B4A app?
 

IslamQabel

Active Member
Licensed User
Longtime User
Erel said:
You can return any html you like. You will need to return a html page with form and then handle the form request with the user name and password.

Note that B4J http server solution is much more powerful and also simpler.

The required issue is http server on the mobile...
 

IslamQabel

Active Member
Licensed User
Longtime User
In the subroutine of HandleMainPage.....
MainPage = MainPage.Replace("$T$", DateTime.time(DateTime.Now))

This will only show the time when the page loaded only...but if i want to show the updated value of the my mobile time what to do??? without refreshing the web page
 
Last edited:

IslamQabel

Active Member
Licensed User
Longtime User
So.....as i understood...should i add some codes to my HTML page that request periodically the server ? or should i add some modifications in the android app?
 

mlc

Active Member
Licensed User
Longtime User

IslamQabel

Active Member
Licensed User
Longtime User
Hello MLC:

thanks for your help.....Actually, i tried using your links before but it does not work with my app variables....so instead of using them, i used the AJAX request and it works.....
Thanks a lot
 
Reactions: mlc

androidtom

Member
Licensed User
Longtime User
I must have something not configured correctly since others seem to be able to compile this example without problems. I have tried various versions of Java from 1.7 to 1.8. Currently my javac is at 1.7.0_79 and I have configured the path in the IDE to point to it. The error I get when compiling the httpserver example is the following:

Compiling generated Java code. Error
javac 1.7.0_79
src\b4a\example\serverservice.java:88: error: cannot find symbol
public static anywheresoftware.b4a.objects.HttpServer _server = null;
^
symbol: class HttpServer
location: package anywheresoftware.b4a.objects
Note: src\b4a\example\serverservice.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

>>>> Problem solved. I re-installed B4a ver 4.3 along with the appropriate libraries. It compiles now. (Not sure what fixed it, but it now works)

What am I missing?
Thank you!!!
 
Last edited:

IslamQabel

Active Member
Licensed User
Longtime User
Hi All...
Does anyone know how to link directly to an image saved in the phone memory or memory card....for example..in the camera example the image was saved with fixed name ...everytime you capture an image it will be saved as follows:
/mnt/sdcard/1.jpg.....when i browse my server i want to put hyperlink that display the image directly when i click it without displaying the other images or contents of my phone how to do that?
 

Ekcentrik

Member
Licensed User
Longtime User
Hi,
I would like to know if it's possible to use HTTPS instead of plain HTTP with this library.
If so , anyone can send me a small example code please?
I would like to send encrypted request on a server(mobile phone).
Thank you for your help.
Marcel
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…