Android Question Load DirInternal file in webview

GERSON PINTO

Member
Licensed User
Hello!
I download the webpage with httpJob (DownloadFile sub)
B4X:
Wait For(DownloadFile("http://www.mypage.htm",File.DirInternal,"mp.htm")) complete(Success As Boolean)

after I want show downloaded page in webview:

B4X:
WebView1.LoadUrl("file://" & File.Combine(File.DirInternal,"mp.htm"))

works fine in emulator but when I run on device (B4A Bridge) show:

net::ERR_FILE_NOT_FOUND

Crazy! What's wrong??? any help
 

GERSON PINTO

Member
Licensed User
Are you checking that success = true?
look my DownloadFile sub:
B4X:
Sub DownloadFile (link As String, Dir As String,filename As String)As ResumableSub
        
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed

    j.Download(link)
    j.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.92 Safari/537.36")
    
    Wait For (j) JobDone(j As HttpJob)
                                
    If j.Success Then
        
        Dim out As OutputStream = File.OpenOutput(Dir,filename, False)
        File.Copy2(j.GetInputStream, out)
        
        out.Close
        
    End If
    
    j.Release
        
    Return j.Success
                
End Sub
 
Upvote 0

GERSON PINTO

Member
Licensed User
Are you checking that success = true?

B4X:
Wait For(DownloadFile("http://www.mypage.htm",File.DirInternal,"mp.htm")) complete(Success As Boolean)
Log (Success)

log:
true

.....

B4X:
WebView1.LoadUrl("file://" & File.Combine(File.DirInternal,"mp.htm"))
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
look my DownloadFile sub:
This is not relevant.
You still need to check it.

This code works properly:
B4X:
Sub Globals
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.b4x.com")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "1.html", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
        WebView1.LoadUrl(xui.FileUri(File.DirInternal, "1.html"))
    End If
    j.Release
End Sub

You can always use LoadHtml with Job.GetString.
 
Upvote 0

GERSON PINTO

Member
Licensed User
This is not relevant.
You still need to check it.

This code works properly:
B4X:
Sub Globals
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.b4x.com")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "1.html", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
        WebView1.LoadUrl(xui.FileUri(File.DirInternal, "1.html"))
    End If
    j.Release
End Sub

You can always use LoadHtml with Job.GetString.

log message:

Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png in data:text/html,chromewebdata (Line: 12)

What is it???
Is it necessary add permissions in manifest or what?
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
you'll be happy to learn that your cf.htm file is downloaded and saved as per y our project (which i have run). however, 2 issues:
1) it apparently cannot be loaded in a webview (which doesn't surprise me since a webview is not a full browser).
2) that said, i have to add that the file that is downloaded by okhttp is NOT the same file you get when you use a regular browser.
if you refer to a portion of the msgbox that i displayed after downloading and saving the file, and if you view the webpage's source with your browser, you will see that they are quite different.

and i see the message which just came in regarding permissions, unless you are changing your code (from your .zip example), there is no issue with permissions. your .zip example runs without error.
 

Attachments

  • pinto.png
    pinto.png
    75.5 KB · Views: 60
Upvote 0

GERSON PINTO

Member
Licensed User
Thanks! Very strange, because if I can view the HTML in the webview (loadUrl) it should be able to download it... anyway what interests me would be only the text. Is there a way to download only the text and sabe It in a file?
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
you have already done that. it's in "cf.html" in your file.dirinternal. you can read it (or display it in a msgbox just like i did). just modify your project a little. you could do all sorts of things with it (print it, email it to yourself, ...) i've seen the whole file. it's 2 giant javascript functions with a tiny html <body> at the end. i was more interestsed in solving your download problem, so i didn't spend any more time with it. out of curiosity, i opened your link on my laptop browser just to see what i should be expecting as a download. surprise! it was not the same as what i downloaded. another problem for another day. in any case, your download code is more or less ok (imho, you could spend more time testing results and logging things. you would then see exactly what's going on instead of waiting until the end to see what happened.)
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
here's your file. 4 lines from the bottom is the key. you need that file.
(i had to rename cf.htm as cf.txt. don't ask why. you can name it back to cf.htm and load it in your browser. it does nothing (except load) because fo the missing file at the end. you'll have to download that file from the original source)
 

Attachments

  • cf.txt
    5.7 KB · Views: 62
Upvote 0

drgottjr

Well-Known Member
Licensed User
and here's what you get if you try to download the missing .js file. maybe it's time to move on to the next project.
 

Attachments

  • erro.png
    erro.png
    54.7 KB · Views: 58
Upvote 0
Top