Android:拦截来自 WebView 的 AJAX 调用 [英] Android: Intercept AJAX call from WebView
问题描述
我想要一个 HTML/javascript
应用程序,在 WebView
中运行,以进行由 Java<处理的
AJAX
调用/code> 代码.
理想的做法是拦截调用(简单,只需使用 shouldOverrideUrlLoading()
)并返回"一些数据.
但是,除了使用 loadUrl()
调用 javascript
函数之外,我没有找到将响应返回"到 WebView
的方法.
这对我不起作用,因为 HTML/javascript
应用程序是我无法控制的插入式应用程序.就 HTML/javascript 应用程序而言,它只是执行一个 AJAX
调用并接收一些数据.
I want a HTML/javascript
application, running in a WebView
to make AJAX
calls that are handled by the Java
code.
Ideal would be to just intercept the call (easy, just use shouldOverrideUrlLoading()
) and 'return' some data.
However, I don't find a way to 'return' a response to the WebView
, other than calling a javascript
function using loadUrl()
.
This will not work for me, as the HTML/javascript
app is a drop-in application which I don't control. As far as the HTML/javascript app concerns, it just does an AJAX
call and receives some data back.
对此有什么想法吗?
推荐答案
最后还是用了问题中描述的方法;
In the end I used the method described in the question anyway;
我拦截了一些特定的请求,例如:foo://bar/get/1?callback=foobar
然后解析 URL
并使用实际的数据.
回调函数是在 URL
的查询部分定义的,所以在上面的例子中就是: foobar();
I intercepted some specific requests like: foo://bar/get/1?callback=foobar
then parsed the URL
and called a javascript callback function with the actual data.
The callback function was defined in the query-part of the URL
, so in the above example that would be: foobar();
调用该函数很简单,只需使用:yourWebView.loadUrl("javascript:foobar('somedata')");
Calling that function was easy, just use: yourWebView.loadUrl("javascript:foobar('somedata')");
这篇关于Android:拦截来自 WebView 的 AJAX 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!