hannna patait
hannna patait

Reputation: 405

Why android webView is not supporting webSocket?

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.

Upvotes: 6

Views: 10831

Answers (3)

Kaeson
Kaeson

Reputation: 11

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">

Upvotes: 0

Harpreet
Harpreet

Reputation: 3070

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);

Upvotes: 0

hannna patait
hannna patait

Reputation: 405

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);

Upvotes: 16

Related Questions