Sub Process_Globals
End Sub
Sub Globals
Private WebViewExtras1 As WebViewExtras
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim WebView1 As WebView
WebView1.Initialize("Webview1")
Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
WebViewExtras1.Initialize(WebView1)
Dim WebViewClient1 As DefaultWebViewClient
WebViewClient1.Initialize("WebViewClient1")
WebViewExtras1.SetWebViewClient(WebViewClient1)
' WebViewExtras1.LoadUrl("http://skysport.tv/update/skycalcio.php")
WebViewExtras1.LoadUrl("http://www.b4x.com/android/forum/threads/webview-how-to-deal-with-a-webresourceresponse.42476/")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub WebViewClient1_ShouldInterceptRequest(Url As String) As WebResourceResponse
Log("WebViewClient1_ShouldInterceptRequest: "&Url)
' in this callback you have 2 options:
' return a WebResourceResponse which the WebView will use instead of requesting the webpage resource
' return Null and the WebView will continue with it's default action requesting the webpage response
Return Null
End Sub
Sub WebViewClient1_ShouldInterceptRequest(Url As String) As WebResourceResponse
Log("WebViewClient1_ShouldInterceptRequest: "&Url)
' in this callback you have 2 options:
' return a WebResourceResponse which the WebView will use instead of requesting the webpage resource
' return Null and the WebView will continue with it's default action requesting the webpage response
Select Url
Case "http://www.b4x.com/android/forum/data/avatars/m/38/38207.jpg?1377605759"
' this should replace Rorry's avatar with a scooby doo image!
Dim WebResourceResponse1 As WebResourceResponse
WebResourceResponse1.Initialize("image/jpeg", Null, File.OpenInput(File.DirAssets, "scooby-doo-96x96.jpg"))
Return WebResourceResponse1
Case Else
' default action - let the webView retrieve the resource
Return Null
End Select
End Sub
hi,warwound, how can i get the respose content? just get the respose that back of server. its an audio. this is the url https://cn.bing.com/translator?ref=TThis&from=vi&to=zh-Hans&isTTRefreshQuery=1 ,and the respose url is https://cn.bing.com/tfettts?isVertical=1&&IG=4C56D437C55E4926BE9219359555AF7A&IID=translator.5023.1 its xmlhttp in pageWith a modified version of my previously posted code i am able to return a WebResourceResponse that in effect loads this thread and replaces your avatar with a scooby doo image!
B4X:Sub WebViewClient1_ShouldInterceptRequest(Url As String) As WebResourceResponse Log("WebViewClient1_ShouldInterceptRequest: "&Url) ' in this callback you have 2 options: ' return a WebResourceResponse which the WebView will use instead of requesting the webpage resource ' return Null and the WebView will continue with it's default action requesting the webpage response Select Url Case "http://www.b4x.com/android/forum/data/avatars/m/38/38207.jpg?1377605759" ' this should replace Rorry's avatar with a scooby doo image! Dim WebResourceResponse1 As WebResourceResponse WebResourceResponse1.Initialize("image/jpeg", Null, File.OpenInput(File.DirAssets, "scooby-doo-96x96.jpg")) Return WebResourceResponse1 Case Else ' default action - let the webView retrieve the resource Return Null End Select End Sub
Martin.
Select Url
Case "https://cn.bing.com/translator?ref=TThis&from=vi&to=zh-Hans&isTTRefreshQuery=1"
Dim WebResourceResponse1 As WebResourceResponse
Dim in As InputStream
Dim urlbytes() As Byte = "https://cn.bing.com/tfettts?isVertical=1&&IG=4C56D437C55E4926BE9219359555AF7A&IID=translator.5023.1".GetBytes("UTF8")
in.InitializeFromBytesArray(urlbytes,0,urlbytes.length)
WebResourceResponse1.Initialize("text/html", "UTF8", in)
Return WebResourceResponse1
Case Else
' default action - let the webView retrieve the resource
Return Null
End Select
thanks,drgottjr, I may not have expressed it very clearly.something like this should work.
B4X:Select Url Case "https://cn.bing.com/translator?ref=TThis&from=vi&to=zh-Hans&isTTRefreshQuery=1" Dim WebResourceResponse1 As WebResourceResponse Dim in As InputStream Dim urlbytes() As Byte = "https://cn.bing.com/tfettts?isVertical=1&&IG=4C56D437C55E4926BE9219359555AF7A&IID=translator.5023.1".GetBytes("UTF8") in.InitializeFromBytesArray(urlbytes,0,urlbytes.length) WebResourceResponse1.Initialize("text/html", "UTF8", in) Return WebResourceResponse1 Case Else ' default action - let the webView retrieve the resource Return Null End Select
1) no guarantees regarding the validity of the url
2) no guarantees regarding the validity of the url for the responserequest
3) no guarantees regarding the validity of the double "&&" in the responserequest. it should probably be a single "&"
4) you say "its xmlhttp in page". i don't know what "xmlhttp" is. if you are referring to an "xmlhttprequest", that is something done through javascript. the documentation for shouldInterceptRequest indicates that it is not used for javascript. if you are sending an xmlhttprequest, you'll have to handle it differently. it may be a challenge to get it to work with shouldInterceptRequest, given the response that shouldInterceptRequest accepts.
5) technically, you should probably pass the responserequest url through a urlencoder before converting to bytes. especially, if you will allow users to use url's of their own devising.
Private Sub wc_LoadResource(Url As String)
If Url.StartsWith("https://cn.bing.com/ttranslatev3?") Then
Log("translated,request audio")
PlayFY 'this is a sub that use javascript to click play audio ico,its worked
Else If Url.StartsWith("https://cn.bing.com/tfettts?") Then
Log("audio has downloaded to webview1")
End If
End Sub