Android的WebViewClient URL重定向(URL的Andr​​oid装载系统) [英] Android WebViewClient url redirection (Android URL loading system)

查看:1196
本文介绍了Android的WebViewClient URL重定向(URL的Andr​​oid装载系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用截获的WebView要求: ShouldInterceptRequest ,里面我用 HttpURLConnection类来从服务器获取数据,我将它设置为跟随重定向,这是透明的webviewclient。这意味着,当我回到WebResponseResource(,,data_inputstream)的WebView maynot知道目标主机的改变。我怎样才能知道这件事发生的的WebView?

I was trying intercept webview request using: ShouldInterceptRequest, inside it I used HttpUrlConnection to fetch data from server, I set it to follow the redirection, which is transparent to webviewclient. This means when I return WebResponseResource("", "", data_inputstream), webview maynot know the target host was changed. How can I tell the webview this happened?

ourBrowser.setWebViewClient(new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view,
                String url) {
                    ..... //some code omitted here

                HttpURLConnection conn = null;
                try {
                    conn = (HttpURLConnection) newUrl.openConnection();
                    conn.setFollowRedirects(true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                   ..... //

                InputStream is = null;
                try {
                    is = conn.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new WebResourceResponse(mimeType, encoding, is);

            }
        }

如果我要求google.com,它应该被重定向到google.co.uk,但web视图不知道重定向,如果CSS文件的链接以co.uk附加为/渲染/ something.css,web视图还是去 http://www.google.com/render/ something.css 找到这应该是CSS文件的http:// www.google.co.uk/render/something.css

If I request "google.com", it should be redirected to "google.co.uk", but webview didnt know the redirection, if the link of css file attaching with "co.uk" is "/render/something.css", the webview still go to "http://www.google.com/render/something.css" to find the css file which should be "http://www.google.co.uk/render/something.css".

任何人都可以帮我吗?

推荐答案

一些元数据(URL,标题等)不能由 WebResourceResponse 类指定。这意味着,你只能使用 shouldInterceptRequest 来提供不同的数据(改变页面的内容),但你不能用它来改变它在加载的URL。

Certain metadata (url, headers, etc..) can't be specified by the WebResourceResponse class. That means that you can only use shouldInterceptRequest to supply different data (change the page's content) but you can't use it to change the URL it's being loaded at.

在你的情况你就吃下了 HttpURLConnection类,这样的WebView中的重定向仍然认为它是装载的 http://www.google.com/ (即使内容是从哪里来的的 http://google.co.uk/ )。如果谷歌的主页上并没有明确设定了基本网址的WebView将继续承担基础URL是 HTTP:// WWW .google.com / (因为它没有看到重定向)。由于相对资源引用(如&LT;链接HREF =//渲染/ something.css/&GT; )的反对BASEURL(在这种情况下是解决< A HREF =htt​​p://www.google.com/相对=nofollow> http://www.google.com/ 而不是的 http://www.google.co.uk/ ),你得到你所观察到的结果。

In your case you're consuming the redirect within the HttpUrlConnection, so the WebView is still thinking that it's loading "http://www.google.com/" (even if the content is coming from "http://google.co.uk/"). If Google's home page doesn't explicitly set a base URL the WebView will continue to assume the base URL is "http://www.google.com/" (since it hasn't seen the redirect). Since relative resource references (like <link href="//render/something.css" />) are resolved against the baseURL (which in this case is "http://www.google.com/" and not "http://www.google.co.uk/") you get the result you observed.

你可以做的就是使用 HttpURLConnection类来找出你要加载的URL是否是一个重定向并返回在这种情况下。不过,我会强烈建议不要在一般使用 HttpURLConnection类 shouldInterceptRequest - web视图的网络堆栈是更有效,而且会取并行(而使用 shouldInterceptRequest 将序列化所有负载在pre-KK网页视图)。

What you could do is use HttpUrlConnection to figure out whether the URL you're going to load is a redirect and return null in that case. However I would strongly advise against using HttpUrlConnection from shouldInterceptRequest in general - the WebView's network stack is much more efficient and will perform fetches in parallel (whereas using shouldInterceptRequest will serialize all of the loads in pre-KK WebViews).

这篇关于Android的WebViewClient URL重定向(URL的Andr​​oid装载系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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