Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top