我的应用程序中有一个WebView,其中打开了一些网站(总是一样,这是我自己的页面).该网站有一些JS代码,可从远程主机加载一些图像.
I have a WebView in my application in which some site is opened (always the same, it is my own page). The site has some JS code which loads some images from the remote host.
我想(通过URL模式)拦截对此类图像的请求并返回我自己的内容(即另一幅图像),或者根据内部应用程序逻辑保持请求不变.
I want to intercept requests to such images (by URL pattern) and give back my own content (i.e. another image), or leave request untouched depending on internal application logic.
有可能这样做吗?
编辑:问题的当前状态...
EDIT: The current state of question...
WebView具有设置WebViewClient的能力(如Axarydax所述). WebViewClient有两种有用的方法
WebView has an ability to set a WebViewClient (as noted by Axarydax). WebViewClient have two useful methods
- shouldOverrideUrlLoading
- onLoadResource
- shouldOverrideUrlLoading
- onLoadResource
shouldOverrideUrlLoading能够拦截任何url加载,如果加载是由页面交互触发的(即,单击页面上的链接,则WebView.loadURL(")不会触发此方法).它还可以通过返回false来取消URL加载.这种方法不可用,因为它无法拦截页面资源的加载(而图像,我需要拦截的是这样的页面资源).
shouldOverrideUrlLoading is able to intercept any url loading, if loading is triggered by page interaction (i.e. link on a page is clicked, WebView.loadURL("") isn't triggering this method). It is also able to cancel URL loading by returning false. This approach isn't usable, cause' it is not able to intercept loading of page resources (and images, what i need to intercept, is such a page resource).
onLoadResource,但是无法取消它.这也使该方法也不适合我的任务.
onLoadResource is triggering every time that page resource (and images! thx to jessyjones) are loading, but there is no way to cancel that. That makes this method not suitable for my task also.
推荐答案API级别11似乎支持您所需要的内容.请参见 WebViewClient.shouldInterceptRequest() .
It looks like API level 11 has support for what you need. See WebViewClient.shouldInterceptRequest().
更多推荐
拦截并覆盖来自WebView的HTTP请求
发布评论