save webview html file

AFSSoftware

Member
Licensed User
That is not possible. For simple Html files you can instead download the html and show it in the WebView with WebView.LoadHtml.

LOL
It's cool to know, but how can i save html file? Show in WebView is very simple, but i want to download it and change the html, so that after the change i can use it in webview ;)
THX 4 HELP
 
Upvote 0

lagore

Active Member
Licensed User
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
 
Upvote 0

kickaha

Well-Known Member
Licensed User
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.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
How are you getting the html into the WebView? If it is off the internet, have a look at this sample app as this downloads a web page and parses it to extract images. You should be able to adapt that.
 
Upvote 0

lagore

Active Member
Licensed User
You need to do a HTTP GET and the HTTP response (if successful) will contain the string you are looking for

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
the above code will return the google web site to the WebView1 and resultstring contains the HTML code which is a string.
Edward
 
Upvote 0

AFSSoftware

Member
Licensed User
You need to do a HTTP GET and the HTTP response (if successful) will contain the string you are looking for

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
the above code will return the google web site to the WebView1 and resultstring contains the HTML code which is a string.
Edward



THX 4 the code. I Think so it will work, but im still to stupide. The sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int) i dont understand. What is httpresponse where do i get a httpresponse from? and the taskid can be whatever...?
Sorry 4 the stupid question... :sign0013:
 
Upvote 0

lagore

Active Member
Licensed User
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
 
Upvote 0

AFSSoftware

Member
Licensed User
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



THX. Pleas Don't think im stupid, but if i debug the code the debugger doesn't go in the two other subs like Sub hc_ResponseError or Sub hc_ResponseSuccess. Because this, nothing happend...
Can you say me how i have to do this... :(
 
Upvote 0

lagore

Active Member
Licensed User
Below is the full code start a new activity and replace everything with the code, add a reference to HTTP in the 'libs'. It creates a webview and editbox changes all occurrences of "google' to "edward" and puts this into the webview, the raw html is displayed in the editbox. You can do anything you want with the string, as it is html it is easy to search for tags and delete sections of code or whatever.
Edward

B4X:
'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
 
Upvote 0

warwound

Expert
Licensed User
Here's an alternative that should do as the OP originally asked.

First you need to use my JSInterface library.
Now you can execute javascript in your WebView from your B4A code and your WebView can also now call a B4A Sub.

B4X:
'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.basic4ppc.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

You can see that the code waits for the PageFinished event before requesting that the web page sends it's HTML content to the B4A Sub 'processHTML'.

This method has an advantage over the previously posted examples - that is when a web page had been dynamically created and it's content is not static.
The user may have submitted a form for example and be viewing a (dynamically generated) web page that cannot be requested using HttpClient.

Martin.
 

Attachments

  • SaveHTML.zip
    11.5 KB · Views: 1,172
Upvote 0

magarcan

Active Member
Licensed User
As I see, there a re two ways to load a web:
-Load from Internet, with WebView1.LoadUrl("http://www.google.com")
-Load HTML code: WebView1.LoadHtml("<html><body>Hello world!</body></html>")

But, if I add a .html file to mi project, can I load it?? I tried with WebView1.LoadHtml(File.DirAssets, "a.htm") but it didn't work.

Thanks!!
 
Upvote 0

canalrun

Well-Known Member
Licensed User
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!!

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.
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
You can also use the WebView LoadUrl method to load a web page from your B4A project files:

B4X:
LoadUrl("file:///android_asset/my_web_page.htm")

Presumably the file protocol could also be used to load web pages from your SD card (just a thought!).

Martin.
 
Upvote 0

mkvidyashankar

Active Member
Licensed User
You need to do a HTTP GET and the HTTP response (if successful) will contain the string you are looking for

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
the above code will return the google web site to the WebView1 and resultstring contains the HTML code which is a string.
Edward


Can we save the resultstring into the html file?
 
Upvote 0
Top