Android Question (WebView) How to deal with a WebResourceResponse

Rorry

Member
Licensed User
Longtime User
Hi there, could someone post an example of how to deal with WebResourceResponse once a shouldInterceptRequest is done. thanks
 

warwound

Expert
Licensed User
Longtime User
Here's an example that shows use of the ShouldInterceptRequest callback/event:

B4X:
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

You'll need my WebViewExtras2 library which can be found here: http://b4a.martinpearman.co.uk/webviewextras/WebViewExtras2-20140318.zip.

Run the example and watch the log, each resource in the webpage causes the ShouldInterceptRequest event to be raised.
The event handler can allow the WebView to continue to request the webpage resource (default behaviour) by returning Null.
Or it can return a WebResourceResponse object which the WebView will use as the webpage resource.

Martin.
 

Attachments

  • ShouldInterceptRequest.zip
    6.5 KB · Views: 319
Upvote 0

Rorry

Member
Licensed User
Longtime User
hi martin.

this is exactly where i am since 2 days,i already watch all the requests in the log window, no problem.
My problem the next step,about the response,

as described in the API reference
http://developer.android.com/refere...est(android.webkit.WebView, java.lang.String)

if i return Null,then the request is normally made, but nothing happen/change if i try to send my own return response with data.
however, im using a sdk version above 11.

how to send the return response and data (a string,a local html file,or whatever...)?

Thank you.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
With 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.
 

Attachments

  • ShouldInterceptRequest.zip
    12.9 KB · Views: 310
Upvote 0

Rorry

Member
Licensed User
Longtime User
Thank you very much ;)

i'll make a donation soon for your libs that i use and your help in this forum.
 
Upvote 0

byz

Active Member
Licensed User
With 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.
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 page
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
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.
 
Upvote 0

byz

Active Member
Licensed User
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.
thanks,drgottjr, I may not have expressed it very clearly.
in fact its bing translator> https://cn.bing.com/translator?ref=TThis&from=zh-Hans&to=en
i want the respose like the gif pictrue show
aqvXLraMtX.gif


in LoadResource event i got the url.
ShouldInterceptRequest(Url As String) As WebResourceResponse not working in b4xpages,but working in b4a.
below is my code:
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

At this point, I don't know what to do next to get the respose data
 
Last edited:
Upvote 0

byz

Active Member
Licensed User
I discovered that Microsoft has a free app for voice intercom. Maybe I won't keep the app going. However, as a technical discussion, I thought I would appreciate it if anyone could tell me if I was able to implement the above and how. Thanks, Erel create b4x
 
Upvote 0
Top