Search code examples
javaandroidandroid-studioandroid-webviewandroid-10.0

Android Q (API level 29) doesn't load HTTPS websites. Gives error: (net::ERR_ACCESS_DENIED)


I was trying fix an issue here on Stackoverflow as the person was trying to make use of WebView to load Facebook's website "https://facebook.com/" but couldn't get it done. I looked at the code and saw that the onReceivedSslError() method was not overridden. I overrode it and tested it out myself on Android emulators running Android 9 (API level 28) and lower and it worked even without adding the cleartextTrafficPermitted however, on testing on Android Q (API level 29), I was getting this error:

net::ERR_ACCESS_DENIED

enter image description here

Here is a copy of the onCreate method:

@SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView webDimrah = findViewById(R.id.WebView1);
        WebSettings webSettingDimrah = webDimrah.getSettings();
        webSettingDimrah.setJavaScriptEnabled(true);
        webDimrah.setWebViewClient(new WebViewClient(){

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });
        webDimrah.loadUrl("https://facebook.com/");

    }

Internet permission has been added to the manifest file. Any suggestions on how to solve this would be highly appreciated :)


Solution

  • Inside your manifest make sure to add android:usesCleartextTraffic="true"

    Android 6.0 introduced the useCleartextTraffic attribute under application element in android manifest. The default value in Android P is “false”. Setting this to true indicates that the app intends to use clear network traffic.