安卓:从截取的WebView AJAX调用WebView、AJAX

2023-09-10 16:26:56 作者:谁人伴我

我希望有一个 HTML / JavaScript的的应用,在的WebView ,使 AJAX运行是由的Java code处理的呼叫。 理想的做法是只截取呼叫(简单,只需使用 shouldOverrideUrlLoading())和回归的一些数据。 但是,我没有找到一种方法,回归到的WebView ,不是调用 JavaScript的其他响应使用功能使用loadURL()。 这不会为我工作,因为 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;

我截取喜欢一些具体要求:富://酒吧/得/ 1回调= foobar的然后分析了网​​址,并要求与实际数据的一个JavaScript回调函数。 回调函数是在 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')");