Is it possible to use okHttp as the webviewclient?

ie mWebView.setWebViewClient(new OkHttpClient());

This gives me an error...

The method setWebViewClient(WebViewClient) in the type WebView is not applicable for the arguments (OkHttpClient)

Any suggestions?

有帮助吗?

解决方案

The WebView doesn't support switching out network stacks if that's what you're asking. Depending on the version it uses a snapshot of the Chrome network stack or for pre-HC devices the WebKit network stack.

Theoretically you could hook up OkHttp via shouldInterceptRequest but I don't recommend doing that - you'll have no way of passing headers to the WebView, you'll have to extract and send cookies manually and it will almost certainly be slower since pre-KK WebView only reads one intercepted request at a time.

其他提示

Considering OkHttpClient extends Object and implements Cloneable and URLStreamHandlerFactory, and WebView.setWebViewClient() accepts only a WebViewClient (which, in turn, extends only Object) there are no common classes that allow this directly.

You may have some luck searching for other folks trying to do this but there doesn't seem to be direct support by OkHttp for this type of drop-in compatibility.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top