适用于 Android 的 WebView 中的 shouldOverrideUrlLoading 未运行 [英] shouldOverrideUrlLoading in WebView for Android not running

查看:22
本文介绍了适用于 Android 的 WebView 中的 shouldOverrideUrlLoading 未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-找到解决方案-
经过一番大量搜索后想通了 - 一个人(我的意思是一个人)说他们改为使用 onPageLoad();这非常适合我的目的.不同之处在于 onPageLoad() 比 shouldOverrideUrlLoading 运行晚,但它对我的代码没有影响.

- Solution Found-
Figured it out after some heavy searching - one person (I literally mean one) said they instead used onPageLoad(); which worked perfectly for my purposes. The difference is that onPageLoad() runs later than shouldOverrideUrlLoading, but It doesn't make a difference in my code.

我正在尝试使用 OAuth 为 Android 应用程序设置 Twitter 授权,到目前为止我可以成功地将用户发送到授权 URL,但是,我现在要做的是拦截重定向到回调(这只会导致 404 错误,我们的回调 URL 在我们的服务器上不会有关联的页面).我试图做的是检查 URL 是否是我们的回调,然后从 URL 中提取 OAuth 验证程序.我使用以下代码设置了我的 WebView:

I'm trying to set up Twitter authorization with OAuth for an Android app, and thus far I can successfully send the user to the authorization URL, however, what I am trying to do now is intercept the redirect to the callback (which would just lead to a 404 error, our callback URL isn't going to have an associated page on our servers). What I'm attempting to do is check if the URL is our callback, then extract the OAuth Verifier from the URL. I setup my WebView with this code:

view = (WebView)findViewById(R.id.twitterWbVw);
view.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView wView, String url)
    {
        String urlHolder;
        String[] verifExtrctr;
        urlHolder = url.substring(0, url.indexOf('?'));
        System.out.println("url");
        if(urlHolder.equalsIgnoreCase(CALLBACK_URL))
        {
            verifExtrctr = urlHolder.split("?");
            verifExtrctr = verifExtrctr[2].split("=");
            if(verifExtrctr[0].equalsIgnoreCase("oauth_verifier"))
            {
                params[5] = verifExtrctr[1];
                return true;
            }
            else
            {
                System.out.println("Inocorrect callback URL format.");
            }
        }
        else    
        {
            wView.loadUrl(url);
        }
        return true;
    }
});
view.loadUrl(urlAuthorize.toExternalForm());

事情是甚至 System.out.println("url");(我用它来调试)不运行!所以我很漂亮想法很枯燥,找不到任何有类似问题的人.授权 URL 顺利通过,我可以成功授权应用程序,但是由于某种原因重定向到回调 URL 永远不会被拦截.任何帮助将不胜感激,如果这很重要,这在我的 onResume() 中.

Thing is even System.out.println("url");(which I'm using to debug)doesn't run! So I'm pretty much dry on ideas, and can't find anyone with a similar problem. The authorization URL goes through fine, and I can successfully authorize the app, however the redirect to the callback URL for some reason never get's intercepted. Any help would be appreciated, this is in my onResume() if that matters.

推荐答案

经过一些研究,我得出结论,尽管大多数教程都这么说,shouldOverrideUrlLoading() 在以下情况下不会被调用:

After some research I conclude that despite what most of the tutorials out there say, shouldOverrideUrlLoading() does not get called when:

  1. 你加载了一个像

  1. You load a URL like

loadUrl("http://www.google.com");

  • 浏览器通过 HTTP 重定向自动重定向用户.(请参阅下面@hmac 关于重定向的评论)

    它确实,当您单击 webview 内的网页内的链接时会被调用.IIRC twitter 授权使用 HTTP 重定向.我认为这是来自一个非常旧版本的 Android API...

    It does however, get called when you you click on a link inside a webpage inside the webview. IIRC the twitter authorization uses an HTTP Redirect.. Bummer, this would be helpful if it worked how all the tutorials say it does. I think this is from a very old version the Android API...

    您可能需要考虑覆盖 WebChromeClientonProgressChanged 方法,如下所示:如何监听一个WebView完成加载一个URL?WebViewClient 的 >onPageFinished() 方法.

    You might want to consider overriding the onProgressChanged method of a WebChromeClient like here: How to listen for a WebView finishing loading a URL? or the onPageFinished() method of the WebViewClient.

    这篇关于适用于 Android 的 WebView 中的 shouldOverrideUrlLoading 未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆