Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

https - shouldOverrideUrlLoading in WebView for Android not running

-Edit: 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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  1. You load a URL like

    loadUrl("http://www.google.com");
    
  2. The browser redirects the user automatically via an HTTP Redirect. (See the comment from @hmac below regarding redirects)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...