无法覆盖 shouldInterceptRequest() 来发出我自己的 HTTP 请求 [英] Unable to override shouldInterceptRequest() to make my own HTTP requests

查看:45
本文介绍了无法覆盖 shouldInterceptRequest() 来发出我自己的 HTTP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android (KitKat) 上使用 WevView,我试图覆盖 shouldInterceptRequest() 并发出我自己的 HTTP 请求 - 让我的应用程序而不是 webview 处理请求.

I'm using a WevView on Android (KitKat) and I'm trying to override shouldInterceptRequest() and make my own HTTP requests - to have the requests handled by my app rather than the webview.

我的观点是解决 webview 的跨域限制,这可能不是一个好主意,但这是另一个问题.

My point is to work around the cross-origin limitation of the webview, which arguably may not be a good idea, but that's another problem.

当我将从 HTTP 请求接收到的数据传递给 shouldInterceptRequest() 方法返回的 WebResourceResponse 对象时,出现问题.在这一点上值得一提的是,它是我的第一个 Android 应用程序(不是从正确的任务开始,但是嘿)而且我对 Android 上的流的了解是有限的.我觉得有一些同步问题,也许 webview 需要缓冲数据而我的 InputStream 没有缓冲?无论如何,尽管在不同的论坛上四处张望,我还是无法弄清楚问题究竟出在哪里.

Something is going wrong when I'm passing the data received from my HTTP request to the WebResourceResponse object returned by the shouldInterceptRequest() method. At this point it's worth mentioning that it's my first Android app (not starting with the right task, but hey) and that my knowledge of streams on Android is limited. I feel like there is some kinf of synchronisation problem, maybe the webview expecting buffered data and my InputStream being unbuffered? Anyway, despite looking around on different forums I can't get my finger on what the problem actually is.

我的代码如下所示:

    mWebView.setWebViewClient( new WebViewClient()
    {
        public WebResourceResponse shouldInterceptRequest(WebView _view, String _url)
        {
            URL url;
            try {
                url = new URL(_url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            }

            HttpURLConnection urlConnection;
            try {
                urlConnection = (HttpURLConnection) url.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }

            WebResourceResponse response = null; 
            try {

                response = new WebResourceResponse( urlConnection.getContentType(), 
                                                    urlConnection.getContentEncoding(),
                                                    urlConnection.getInputStream() );
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                urlConnection.disconnect();                 
            }

            return response;
        }
    });

当我运行我的测试应用程序时,它崩溃了.Logcat 给了我以下日志:

When I run my test app, it crashes. Logcat gives me the following logs:

W/System.err(4527): java.io.IOException: stream closed
W/System.err(4527):     at com.android.okhttp.internal.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68)
W/System.err(4527):     at com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream.available(HttpTransport.java:476)
W/System.err(4527):     at dalvik.system.NativeStart.run(Native Method)
A/libc(4527): Fatal signal 6 (SIGABRT) at 0x000011af (code=-6), thread 4551 (example.webtest)
I/chromium(4527): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported

我看到有人试图做类似的事情的帖子(系统崩溃在 WebViewClient 中重写 shouldInterceptRequest 时)但我遇到的错误似乎完全不同.

I have seen a post by someone trying to do something similar (System Crash When Overriding shouldInterceptRequest in WebViewClient) but the error I encounter seems completely different.

有什么想法吗?

感谢您的帮助!

推荐答案

我遇到了同样的问题.如果删除

I had the same issue. If you remove the

urlConnection.disconnect();

它有效.所以原因一定是在检索内容之前关闭了连接.但是我不确定为什么会发生这种情况,我的猜测是 WebResourceResponse 在流上做了一些懒惰的读取.

it works. So the reason must be the connection is closed before the content is retrieved. I am not sure however as to why that happens, my guess is WebResourceResponse does some lazy reading on the stream.

我的解决方案只有在 WebResourceResponse 本身关闭流时才干净",否则连接和流不会关闭并泄漏资源.

My solution is only 'clean' if WebResourceResponse closes the stream itself, otherwise the connection and stream wouldn't get closed and leak resources.

这篇关于无法覆盖 shouldInterceptRequest() 来发出我自己的 HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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