Android Question B4A embedded server

apty

Active Member
Licensed User
Longtime User
I am trying to use the httpserver library. When I load the files from another directory apart from DirAssets, it doesn't work. For example, when I use the code below:
B4X:
Sub GetTemplate(Name As String) As String
    If templates.ContainsKey(Name) Then Return templates.Get(Name)
    Dim temp As String = File.ReadString(rp.GetSafeDirDefaultExternal("")&"/"&Main.linkname, Name)
    Log("temp"&temp)
    templates.Put(Name, temp)
    Return temp
End Sub
It gives an error that "Error serving request: (FileNotFoundException) java.io.FileNotFoundException: newlink\index.html"

Does it only work with DirAssets? How can I make it work with other directories?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Error has nothing to do with HttpServer.

As a general rule: no reason to use the external storage. Best to use File.DirInternal = XUI.DefaultFolder.

If you do want to use the external storage then the subfolder must be set like this:
B4X:
Dim temp As String = File.ReadString(rp.GetSafeDirDefaultExternal(Main.linkname), Name)
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
i have tried using File.DirInternal and i also tried setting subfolder with
B4X:
File.ReadString(rp.GetSafeDirDefaultExternal(Main.linkname), Name)
but in both cases, i get the same error. If I use File.DirAssets, it works perfectly. I have confirmed the files exist in above locations by listing contents of the directories. I have also noted that if the file still exists in File.DirAssets, then the server points to that file even if I set
B4X:
Dim temp As String = File.ReadString(rp.GetSafeDirDefaultExternal(Main.linkname), Name)
. But if I remove the file from DirAssets, it now shows the error, though my application doesn't point to DirAssets
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
i am trying to load the attached html game since it doesn't work directly on webview unlesss it is served from a server. My Code is as below (the rest of the code is as in the example httpserver project):
B4X:
Sub GetTemplate(Name As String) As String
    If templates.ContainsKey(Name) Then Return templates.Get(Name)
    Dim temp As String = File.ReadString(File.DirInternal&"/"&Main.linkname, Name)
    Log("temp"&temp)
    templates.Put(Name, temp)
    Return temp
End Sub

Sub HandleMainPage (Response As ServletResponse)
    Dim MainPage As String = GetTemplate("index.html") 'load the template from the assets folder
    Response.SetContentType("text/html")
    Response.SendString(MainPage)

End Sub
then on the webview it is as below:
B4X:
StartService(ServerService)  
Sleep(4000)
Dim ssocket As ServerSocket
 WebView1.LoadUrl(ssocket.GetMyWifiIP & ":" & ServerService.port)

After running this, I get the below error:
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: style.css
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: scripts\main.js
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: scripts\supportcheck.js

etc for each file in the project

html project file=>html project
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
I copied them then unzipped using ArchiverPlusZip library. If i check using File.ListFiles, i could see the files
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Are you using xHttpServer?
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
The value of Main.linkname is game2 (name of the unarchive directory for the contents) and full logs are as shown below:

B4X:
*** Service (serverservice) Create ***
2021-07-04 14:27:15.533:INFO::jetty-7.x.y-SNAPSHOT
2021-07-04 14:27:15.552:INFO::started o.e.j.s.ServletContextHandler{/,null}
2021-07-04 14:27:15.556:INFO::Started [email protected]:6759 STARTING
org.eclipse.jetty.server.Server@1b93ce8
** Service (serverservice) Start **
Client: 192.168.100.24
/

Client: 192.168.100.24
/style.css
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: style.css
Client: 192.168.100.24
/scripts/main.js
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: scripts\main.js
Client: 192.168.100.24
/scripts/supportcheck.js
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: scripts\supportcheck.js
Client: 192.168.100.24
/scripts/offlineclient.js
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: scripts\offlineclient.js
Client: 192.168.100.24
/scripts/register-sw.js
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: scripts\register-sw.js
Client: 192.168.100.24
/scripts/main.js
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: scripts\main.js
Client: 192.168.100.24
/icons/icon-512.png
Error serving request: (FileNotFoundException) java.io.FileNotFoundException: icons\icon-512.png
** Receiver (s1) OnReceive **
*** Service (s1) Create ***
** Service (s1) Start **
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
Are you using xHttpServer?
No, i am using the old httpserver library
 
Upvote 0
Top