Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In my application I am using webView to call a URL. My website is using web Socket to change values. when i open same URL in chrome application it's value changes it means web socket is working fine. But, inside webView value change is not happening. Is it mean web Socket is not supporting in webView widget. Where, I have noticed that WebViewClient's onLoadResource() method keeps calling infinite times.

Web socket did not work because local storage is disabled by default.

Enabling it in my Android WebView solved the issue.

webView.getSettings().setDomStorageEnabled(true);
                Hi, so does this mean websocket is working normally in your webview app now? I ask because in the older question (stackoverflow.com/questions/13507438/…) on this topic, the accepted answer says it is not supported. So I want to be sure before starting. Thanks!
– dakini
                Mar 19, 2021 at 1:44

For my case, I had to enable AppCache as well. It working fine now.

final WebSettings settings = web.getSettings();
    settings.setLoadsImagesAutomatically(true);
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setAppCacheEnabled(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        settings.setSafeBrowsingEnabled(false);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    web.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CookieManager.getInstance().setAcceptThirdPartyCookies(web, true);
    // Extras tried for Android 9.0, can be removed if want.
    settings.setAllowContentAccess(true);
    settings.setAllowFileAccess(true);
    settings.setBlockNetworkImage(false);

Check your error code first; if the code is: ERR_CLEARTEXT_NOT_PERMITTED try to add this to your application tag in your AndroidManifest.xml like below:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
                Hi, after doing this, does websocket work on webview apps? The older answers say it is not supported, so I am a little confused.
– dakini
                Mar 19, 2021 at 1:46
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.