Android Question How to load a local html file with httpserver?

watesoft

Active Member
Licensed User
Longtime User
I open a local html file using webview, and there is no problem using the file protocol. But for the sake of JavaScript, I have to use the http protocol, so I try to use old httpserver lib, but it cannot open html files. I am very confused about httperver and have little involvement in web apps. Can anyone help me see what the reason is? Thank you!

httpserver:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    server.Initialize("server")
    server.Start(50551)
    WebSet.setAllowFileAccess(WebView1,True)
    Dim jo As JavaObject =WebView1
    Dim settings As JavaObject = jo.RunMethod("getSettings", Null)
    Dim r As Reflector
    r.Target = settings
    r.RunMethod2("setAllowUniversalAccessFromFileURLs", True, "java.lang.boolean")

End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'It can open index.html with file protocol,but blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
    WebView1.LoadUrl($"${xui.FileUri(File.DirInternal & "/yux_storage/", "index.html")}"$)
    
    'so I try to use httpserver to open local html file,but it can't open index.html.
    WebView1.LoadUrl("http://127.0.0.1:50551"  & File.DirInternal & "/yux_storage/index.html")
End Sub

Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    'I think there should be something added here, but I don't know how to write it.
End Sub
 

Attachments

  • yux-storage.zip
    260.1 KB · Views: 127

ilan

Expert
Licensed User
Longtime User
this works:

B4X:
Private Sub Button1_Click
    'It can open index.html with file protocol,but blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
    'WebView1.LoadUrl($"${xui.FileUri(File.DirInternal & "/yux_storage/", "index.html")}"$)
  
    'so I try to use httpserver to open local html file,but it can't open index.html.
    'WebView1.LoadUrl("http://127.0.0.1:50551"  & File.DirInternal & "/yux_storage/index.html")
  
    Dim html As String = File.ReadString(File.DirInternal, "/yux_storage/index.html")
    WebView1.LoadHtml(html)
End Sub

no server needed!
 
Last edited:
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
this works:

B4X:
Private Sub Button1_Click
    'It can open index.html with file protocol,but blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
    'WebView1.LoadUrl($"${xui.FileUri(File.DirInternal & "/yux_storage/", "index.html")}"$)
 
    'so I try to use httpserver to open local html file,but it can't open index.html.
    'WebView1.LoadUrl("http://127.0.0.1:50551"  & File.DirInternal & "/yux_storage/index.html")
 
    Dim html As String = File.ReadString(File.DirInternal, "/yux_storage\index.html")
    WebView1.LoadHtml(html)
End Sub

no server needed!
Thank your! but this is just a simple example, I actually have some complex js that need to be loaded, and the readstring method can't load these js, and some requests within js will produce cross-domain errors.
so I only want to know how to open local html page with httpserver.
 
Last edited:
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
I open a local html file using webview, and there is no problem using the file protocol. But for the sake of JavaScript, I have to use the http protocol, so I try to use old httpserver lib, but it cannot open html files. I am very confused about httperver and have little involvement in web apps. Can anyone help me see what the reason is? Thank you!

I solved it myself!
Server_HandleRequest:
Private Sub Button1_Click
    WebView1.LoadUrl("http://127.0.0.1:50551")
End Sub

Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    Dim RootDir As String=File.Combine(File.DirInternal,"/index.html")
    Dim requestFile As String=Request.RequestURI
    If File.Exists(RootDir,requestFile) Then
        Dim filecontent As String = File.ReadString(RootDir,requestFile)
        Response.SendString(filecontent)
    End If
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
you want to read a local html file and you use a server to send the string to the same device using Server_HandleRequest ?
why?

what is the difference reading the html directly as string to sending the string via SendString function?
if another device (client) connects to the phone and get the html from the phone that acts as a server then i understand why to do it like this but if you have the html already why do u use a server object?
 
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
you want to read a local html file and you use a server to send the string to the same device using Server_HandleRequest ?
why?

what is the difference reading the html directly as string to sending the string via SendString function?
if another device (client) connects to the phone and get the html from the phone that acts as a server then i understand why to do it like this but if you have the html already why do u use a server object?

Hi ilan,thanks for your advice.Let me explain, the HTML and JavaScript of the main function module were not written by me, but were downloaded from the open source website. I just packaged it as APK using B4A. The author reminds me that a local server is needed to run this program completely, as both HTML and JavaScript both need local file requests, so I used HTTP server. I hardly write web apps, so I'm not familiar with network protocols, so the questions I ask are a bit silly. I configured it in the HandleRequest and now it can run completely.​

 
Upvote 0
Top