3

I have an android application, contains WebView which allow users to login through OAuth2 Implicit Flow technique.

The main issue here, is that the webview didn't load the needed page and instead it shown a blank page.

The log-cat didn't log any helpful info.

Is it any issue with nought (API 24, 25) and WebView.

My code works correctly in the other versions.

  public void initializeWebView(final WebView webView,
                                       final WebError webError,
                                       final String authUrl)
{

    if(pref.getData("accesstoken") != null && !TextUtils.isEmpty (pref.getData("accesstoken")) )
    {
        // Check if accesstoken valid then redirect.

        new Handler().postDelayed(new Runnable() {
            public void run() {
                NavigatonHelper.navigateToAnotherActivity(currentContext, MainActivity.class,false);
            }
        },  1000);
    }
    else{

        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                super.onPageStarted(view, url, favicon);
                currentFragment.showLoading();
            }


            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.equals("https://...")) {
                    navigatonHelper.ReplacePlaceHolderWithFragment(R.id.auth_fragment_placeholder,currentContext,new RegistrationFragment(),null);
                    return true; // Handle By application itself
                }else{

                    view.loadUrl(url);
                    return false;
                }
            }

            @TargetApi(android.os.Build.VERSION_CODES.M)
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return shouldOverrideUrlLoading(view, request.toString());
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                currentFragment.hideLoading();

                String accessToken = "";
                String idtoken = "";

                if(url.contains("id_token="))
                {
                    Pattern idTokenPattern = Pattern.compile("id_token=(.*?)&");
                    Matcher idTokenMatcher = idTokenPattern.matcher(url);
                    while (idTokenMatcher.find()) {
                        idtoken = idTokenMatcher.group(1);
                    }

                    pref.saveData("idtoken",idtoken);
                }

                if (url.contains("&access_token="))
                {
                    Pattern pattern1 = Pattern.compile("&access_token=(.*?)&");
                    Matcher matcher1 = pattern1.matcher(url);
                    while (matcher1.find()) {
                        accessToken = matcher1.group(1);
                    }
                    pref.saveData("accesstoken", accessToken);
                }

                if(pref.getData("accesstoken") != null && !TextUtils.isEmpty (pref.getData("accesstoken")) )
                {
                    if(url.contains("Callback")){

                        // load static page
                        webViewHelper.generateBlankHtmlPage(webView);
                    }

                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            NavigatonHelper.navigateToAnotherActivity(currentContext, MainActivity.class,false);
                        }
                    },  1000);
                }

            }

            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view,errorCode,description,failingUrl);

                // Handle the error
                webError.setWebViewErrorFired(true);
                webError.setCode(errorCode);
                webError.setUrl(failingUrl);
            }

            @TargetApi(android.os.Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
                // Redirect to deprecated method, so you can use it in all SDK versions
                onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
            }

        });

        // load page
        webView.loadUrl(authUrl);
    }
}

I found this but it didn't help: SO Question

Community
  • 1
  • 1
Marzouk
  • 2,650
  • 3
  • 25
  • 56

0 Answers0