如何在Java中使用WebResourceRequest获取WebView的Url?

时间:2017-06-07 11:54:38

标签: java android android-studio webview

我有这些代码:

   public class MyAppWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            //want url of the webview in here
        }

我只是想知道,如何获取webView的Url来覆盖Url加载。 {1}}方法在API21中被删除。

提前致谢!

4 个答案:

答案 0 :(得分:6)

\ Android \ sdk \ sources \ android-25 \ android \ webkit \ WebViewClient.java 中,您可以看到此代码。

你的问题答案很简短:      request.getUrl().toString()

检查GrepCode上的来源。

答案 1 :(得分:3)

API 24中已弃用{p> boolean shouldOverrideUrlLoading (WebView view, String url),API级别24中添加了boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request)

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if(Uri.parse(request.getUrl().toString()).getHost().endsWith("google.com")) {
                //The host application wants to leave the current WebView and handle the url itself. 
                return true;
            }

            if(Uri.parse(request.getUrl().toString()).getHost().endsWith("yandex.com")) {                    
                //The current WebView handles the url. 
                return false;
            }               

            if(Uri.parse(request.getUrl().toString()).getScheme().equals("file")) {
                //The current WebView handles the url. 
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
            view.getContext().startActivity(intent);
            return true;
        }

答案 2 :(得分:0)

请看我的回答可能会对你有所帮助...... !!!!

WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                System.out.println("your current url when webpage loading.." + url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                System.out.println("your current url when webpage loading.. finish" + url);
                super.onPageFinished(view, url);
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onLoadResource(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                System.out.println("when you click on any interlink on webview that time you got url :-" + url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

how to get the current page url from the web view in android

答案 3 :(得分:0)

--use-database-names

然后你可以随心所欲地使用它。