Android Naught没有在我的应用程序中运行WebView

时间:2017-02-24 20:20:56

标签: android webview android-7.0-nougat

我有一个Android应用程序,包含允许用户通过OAuth2 Implicit Flow技术登录的WebView。

这里的主要问题是webview没有加载所需的页面,而是显示了一个空白页面。

log-cat没有记录任何有用的信息。

是否存在任何问题(API 24,25)和WebView。

我的代码在其他版本中正常运行。

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

我找到了这个,但没有帮助: SO Question

0 个答案:

没有答案