is it possible to save webview html file for offline reading
That is not possible. For simple Html files you can instead download the html and show it in the WebView with WebView.LoadHtml.
Surely it is just a case of saving the string that the webview is loaded with,
WebView1.LoadHtml(resultString),
resultString is just a text string that is the html source code of the web page (all be it a long string). This string can then be manipulated like any string, remove parts, search for tags etc then the modified string can be saved and later loaded into the webview again and any correctly formatted html will show as web page.
Edward
Will not work as LoadHtml only loads a string, it does not return a string.
Dim hc As HttpClient
Sub GetRemoteMessages
Dim req As HttpRequest 'Set up an http request connection
PostUrl ="http://www.google.com"
req.InitializeGet(PostUrl)
hc.Execute(req, 1)
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int) 'We got connection and data !!
Dim resultString As String
resultString = Response.GetString("UTF8") 'This holds the returned data
WebView1.LoadHtml(resultString)
response.Release
End Sub
You need to do a HTTP GET and the HTTP response (if successful) will contain the string you are looking for
the above code will return the google web site to the WebView1 and resultstring contains the HTML code which is a string.B4X:Dim hc As HttpClient Sub GetRemoteMessages Dim req As HttpRequest 'Set up an http request connection PostUrl ="http://www.google.com" req.InitializeGet(PostUrl) hc.Execute(req, 1) End Sub Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int) 'We got connection and data !! Dim resultString As String resultString = Response.GetString("UTF8") 'This holds the returned data WebView1.LoadHtml(resultString) response.Release End Sub
Edward
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int) 'No connection :-(
Log("Error connecting: " & Reason & " " & StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
ProgressDialogHide
Response.Release
End If
End Sub
The sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int) is a member of hc.Execute(req, 1) the same way if you define a button <Dim GoBtn as Button> the sub GoBtn_Click is a member of your button.
When hc.Execute gets a successful return from the web it will raise the hc_ResponseSuccess sub, like wise if it is unsuccessful it will raise the an error the following sub should be included to catch that error.
B4X:Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int) 'No connection :-( Log("Error connecting: " & Reason & " " & StatusCode) If Response <> Null Then Log(Response.GetString("UTF8")) ProgressDialogHide Response.Release End If End Sub
TaskID is an Int in the example above hc.Execute(req, 1) it is set to 1, you can use this ID in the response code to handle different requests. If your code is only ever going to do one request then set it at 1 and you do not need to do and checking on the TaskID in the response sub.
Edward
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim hc As HttpClient
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
hc.Initialize("hc")
Dim WebView1 As WebView
Dim textbox1 As EditText
Dim lv As LayoutValues
lv = GetDeviceLayoutValues
End Sub
Sub Activity_Create(FirstTime As Boolean)
WebView1.Initialize("")
textbox1.Initialize("")
Activity.AddView(WebView1,10dip,10dip,lv.Width-10,(lv.Height/2)-10)
Activity.AddView(textbox1,10dip,lv.Height/2,lv.Width-10,(lv.Height/2)-10)
GetWebPage
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub GetWebPage
Dim req As HttpRequest 'Set up an http request connection
Dim PostUrl As String 'This will hold the url
PostUrl ="http://www.google.com"
req.InitializeGet(PostUrl) 'Initialize the http get request
ProgressDialogShow("Getting Messages...") 'Let The user know what is going on.
hc.Execute(req, 1)
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int) 'We got connection and data !!
ProgressDialogHide 'Close the waiting message..
Dim resultString As String
Dim webstring As String
resultString = Response.GetString("UTF8") 'This holds the returned data
webstring = resultString.Replace("Google", "Edward") 'This will change google on the web to edward
textbox1.Text=resultString
WebView1.LoadHtml(webstring )
response.Release
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int) 'No connection :-(
Log("Error connecting: " & Reason & " " & StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
ProgressDialogHide
Response.Release
End If
End Sub
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim myInterface As JSInterface
Dim WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layoutMain")
WebView1.Width=100%x
WebView1.Height=100%y
' add the B4A javascript interface to the WebView
myInterface.addJSInterface(WebView1, "B4A")
' now load a web page
WebView1.LoadUrl("http://www.b4x.com/android/help/jsinterface.html")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub WebView1_PageFinished (Url As String)
' Now that the web page has loaded we can get the page content as a String
Dim jsStatement As String
jsStatement="B4A.CallSub('processHTML', document.documentElement.outerHTML)"
Log("PageFinished: "&jsStatement)
myInterface.execJS(WebView1, jsStatement)
End Sub
Sub processHTML(html As String)
' This is the Sub that we'll get the web page to send it's HTML content to
' Log may truncate a large page so you'll not see all of the HTML in the log but the 'html' String should still contain all of the web page HTML
Log("processHTML: "&html)
End Sub
But, if I add a .html file to my project, can I load it?? I tried with WebView1.LoadHtml(File.DirAssets, "a.htm") but it didn't work.
Thanks!!
Dim s As String
s = File.GetText(File.DirAssets, "myfile.html")
wvMain.LoadHtml(s)
Thank you very very much!I use the following code, which someone gave me.
B4X:Dim s As String s = File.GetText(File.DirAssets, "myfile.html") wvMain.LoadHtml(s)
I include myfile.html in the project using the file tab.
Barry.
LoadUrl("file:///android_asset/my_web_page.htm")
You need to do a HTTP GET and the HTTP response (if successful) will contain the string you are looking for
the above code will return the google web site to the WebView1 and resultstring contains the HTML code which is a string.B4X:Dim hc As HttpClient Sub GetRemoteMessages Dim req As HttpRequest 'Set up an http request connection PostUrl ="http://www.google.com" req.InitializeGet(PostUrl) hc.Execute(req, 1) End Sub Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int) 'We got connection and data !! Dim resultString As String resultString = Response.GetString("UTF8") 'This holds the returned data WebView1.LoadHtml(resultString) response.Release End Sub
Edward