Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 364
Shmuel Avatar asked Mar 14 '14 17:03

Shmuel


People also ask

How do I use OkHttp on Android?

OkHttp Query Parameters ExampleBuilder urlBuilder = HttpUrl. parse("https://httpbin.org/get).newBuilder(); urlBuilder. addQueryParameter("website", "www.journaldev.com"); urlBuilder. addQueryParameter("tutorials", "android"); String url = urlBuilder.

Where is OkHttp used?

OkHttp is an HTTP client from Square for Java and Android applications. It's designed to load resources faster and save bandwidth. OkHttp is widely used in open-source projects and is the backbone of libraries like Retrofit, Picasso, and many others.

Is OkHttp asynchronous?

OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.


1 Answers

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.

like image 78
marcin.kosiba Avatar answered Oct 05 '22 08:10

marcin.kosiba