Android,如何从 WebView 检索重定向 URL? [英] Android, How to retrieve redirect URL from a WebView?

查看:36
本文介绍了Android,如何从 WebView 检索重定向 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我在网上冲浪,但没有找到与我的情况相符的答案.我想从 webview 中获取特定的重定向 url,然后阻止 webview 打开它.案例是用户授权我的应用程序后的 Instagram 重定向 url.怎么做?这是我的代码:

First i surfed the web but no answer was found matching my case. I want to grab a specific redirect url from a webview and then keep the webview from opening it. The case is an Instagram redirect url after the user authorized my app. How to do it? Here is my code:

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("https://api.instagram.com/oauth/authorize/?client_id=my_client_id&redirect_uri=my_redirect_uri&response_type=code");

推荐答案

最后感谢 Udi I 对我的问题的回答,稍作改动,我设法找到了问题的解决方案.这是对我有用的代码:

Finally thanks to the answer given to my question by Udi I, with a little change, i managed to find a solution to the problem. Here is the code which worked for me:

    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView wView, String url) {
               return (url.indexOf("some part of my redirect uri") > -1);                               
        }       
    });
    webView.loadUrl(myUrl); //myUrl is the initial url.

使用上面的代码,如果有任何包含重定向uri的url,webView不会加载它.否则,webView 将加载它.还要感谢 Kamil Kaminski.

Using the above code, if there will be any url containing redirect uri, webView won't load it. Else, webView will load it. Also thanks to Kamil Kaminski.

这篇关于Android,如何从 WebView 检索重定向 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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