質問

Is there any possibility to use Androids WebView only for rendering, not for doing all the communication stuff? I would prefer to handle communication on my own using for example the Apache HttpClient with all its possibilities when it comes to handling certificates and so on. But I run into problems with links and forms clicked/executed from the WebView. I intercept these requests with a custom WebViewClient and shouldInterceptRequest and call my own communication component to fetch the HTML. If my communication component followed redirects how can I inform my WebView about the current base URL? Up to now relative paths in the HTML won't work because the WebView still thinks the old base URL from the original request is still active and I can only pass a WebResourceResponse back to the WebView which only contains the HTML data. Is there any other component that could do the rendering without caring about the communication? If not, what do I have to do to customize the WebView to work for my use case? Thanks!

Best regards Bjoern

役に立ちましたか?

解決 2

I will answer my own question to provide additional information if somebody else needs a similar strange setup as mine :-) The answer provided by user3431672 is one part of the solution. This ensures that no stuff is loaded from the Internet by the WebView.

Additionaly I created a custom WebViewClient and implemented the shouldInterceptRequest which fires at least for all GET requests. My own communication component fetches the data and returns the method with a WebResourceResponse if no redirect was followed. If there was a redirect during fetching the data for the request I don't return the WebResourceResponse. Instead I call loadDataWithBaseURL(...) and return the shouldInterceptRequest() method with null.

To be able to handle POST requests I had to implement a custom class to inject JavaScript and add it to the WebView via addJavaScriptInterface(). With this class I register an EventListener on all button elements and, when clicked, go through all the input elements (and whatever else I need) to parse my data for the POST request. The POST request is then handled by my communication component and the response is again loaded via loadDataWithBaseURL(...).

Hope this helps somebody.

他のヒント

You can use webview.getSettings().setBlockNetworkLoads(true) to achieve this.

http://developer.android.com/reference/android/webkit/WebSettings.html#setBlockNetworkLoads(boolean)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top