Android WebView,如何在应用程序中处理重定向而不是打开浏览器 [英] Android WebView, how to handle redirects in app instead of opening a browser

查看:43
本文介绍了Android WebView,如何在应用程序中处理重定向而不是打开浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以现在在我的应用程序中,我正在访问的 URL 有一个重定向,当发生这种情况时,WebView 将打开一个新的浏览器,而不是留在我的应用程序中.有没有办法可以更改设置,以便视图像平常一样重定向到 URL,但留在我的应用程序中而不是打开新浏览器?

So right now in my app the URL I'm accessing has a redirect, and when this happens the WebView will open a new browser, instead of staying in my app. Is there a way I can change the settings so the View will redirect to the URL like normal, but stay in my app instead of opening a new browser?

我想要重定向 URL,我只是不知道如何创建它,所以访问该 URL 的唯一方法是通过一个会导致重定向到我想要的 URL.

I want the redirecting URL, I just don't know how to create it, so the only way to get to that URL is through one that will cause a redirect to the one I want.

例如:当您访问这里时:http://www.amazon.com/gp/aw/s/ref=is_box_/k=9780735622777 注意它如何将 URL 重定向到实际产品.在我的应用程序中,如果我在新浏览器中打开它,它会做得很好,但是如果我将它与 WebView 一起保存在我的应用程序中,它将显示为好像在搜索 k=9780735622777,如下所示:http://www.amazon.com/gp/aw/s/ref=is_s_?k=k%3D9780735622777&x=0&y=0.或者,它将在浏览器中打开视图并显示合适的内容.但是,我想将所有内容保留在我的应用中.

For example: When you go here: http://www.amazon.com/gp/aw/s/ref=is_box_/k=9780735622777 notice how it will redirect the URL to the actual product. In my app, if I open it in a new browser, it will do that just fine, however if I keep it in my app with a WebView, it will show up as though it's doing a search for k=9780735622777, like this: http://www.amazon.com/gp/aw/s/ref=is_s_?k=k%3D9780735622777&x=0&y=0 . OR, it will open the view in the browser and show what is appropriate. However, I want to keep everything in my app.

推荐答案

创建一个 WebViewClient,并覆盖 shouldOverrideUrlLoading 方法.

Create a WebViewClient, and override the shouldOverrideUrlLoading method.

webview.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        // do your handling codes here, which url is the requested url
        // probably you need to open that url rather than redirect:
        view.loadUrl(url);
        return false; // then it is not handled by default action
   }
});

这篇关于Android WebView,如何在应用程序中处理重定向而不是打开浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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