Search code examples
androidwebviewwebviewclientokhttp

Android - Use OkHttp with Webview


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?


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.