skip to Main Content

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 🙂

2

Answers


  1. 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.

    Login or Signup to reply.
  2. You have to use android:usesCleartextTraffic in you manifest.

    This flag is ignored on Android 7.0 (API level 24) and above if an Android Network Security Config is present.

    Please check the below explanation from Developer site for more details.

    https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search