如何异步调用 WebViewClient.shouldInterceptRequest

2023-11-26

我想创建一个 Intranet 应用程序。该应用程序将显示通常只能在我们的内部环境中访问的内容。 例如http://intranet.ourfirm.com

现在我们可以从外部访问此内容 例如https://ourproxy.com/ourIntranetApplicationID/(这将被定向到http://intranet.ourfirm.com)

我更改了每个原始网址,例如http://intranet.ourfirm.com/whatever/index.html to https://ourproxy.com/ourIntranetApplicationID/whatever/index.html.

在index.htm 中,多个资源以绝对或相对方式定义。 我将它们全部设置为绝对并将它们转换为我们的代理 url(请参阅 *1 )(可从我们公司外部的任何地方访问)

这一切都运行得很完美,但有一个大问题。它慢得像地狱一样! 转换过程是在我的 MyWebViewClient.shouldInterceptRequest 方法中启动的。

我的 html 有 80 个要加载的资源,并且为每个资源顺序调用 shouldInterceptRequest:

public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        LOGGER.debug("ENTER shouldInterceptRequest: " + String.format("%012d", interceptCounter.incrementAndGet()));
        WebResourceResponse response;


    HttpURLConnection conn;

        try {
            conn = myRewritingHelper.getConnection(request.getUrl(), method); // *1 this internally converts the url and gets a connection adds the header for Basic Auth etc.

            // add request headers
            for (Map.Entry<String, String> entry : request.getRequestHeaders().entrySet()) {
                conn.setRequestProperty(entry.getKey(), entry.getValue());
            }

            // Read input
            String charset = conn.getContentEncoding() != null ? conn.getContentEncoding() : Charset.defaultCharset().displayName();
            String mime = conn.getContentType();
            InputStream is = conn.getInputStream();



            long interceptStopTimestamp = System.currentTimeMillis();
            long durationIntercepting = interceptStopTimestamp - interceptStartTimestamp;
            LOGGER.info("InterceptionDuration : " + durationIntercepting);

            // *2 we have to define null for the mime-type , so the WebResourceResponse gets the type directly from the stream
            response = new WebResourceResponse(null, charset, isContents);
        } catch (IllegalStateException e) {
            LOGGER.warn("IllegalStateException", e);

        } catch (IOException e) {
            LOGGER.warn("IOException: Could not load resource: " + url, e);
        }


        LOGGER.debug("LEAVE shouldInterceptRequest: " + String.format("%012d", interceptCounter.get()));
        return response;
    }

正如您所看到的,我在拦截方法的开头使用了 AtomicInteger 增量和日志记录, 并在方法结束时记录该值。

它总是记录:

ENTER shouldInterceptRequest:  000000000001
LEAVE shouldInterceptRequest:  000000000001
ENTER shouldInterceptRequest:  000000000002
LEAVE shouldInterceptRequest:  000000000002
ENTER shouldInterceptRequest:  000000000003
LEAVE shouldInterceptRequest:  000000000003
ENTER shouldInterceptRequest:  000000000004
LEAVE shouldInterceptRequest:  000000000004
:
:
ENTER shouldInterceptRequest:  000000000080
LEAVE shouldInterceptRequest:  000000000080

这样我就能够检查 shouldInterceptRequest() 方法永远不会异步启动。 如果该方法被异步调用,则在前一个数字的 LEAVE 发生之前,会出现一个更大的数字@ENTER-Comment。 不幸的是,这从未发生过。

对 myRewritingHelper.getConnection() 的调用是非锁定的。

现在我的问题:是否有可能激发 WebviewClient 异步调用其 shouldInterceptRequest() 方法? 我非常确定,如果可以异步加载 Web 视图的多个资源,这将极大地提高性能! Web 视图按顺序加载资源。

一个有趣的子问题是,为什么我必须将 Web 资源创建中的 mime-type 定义为 0(参见 *2)。 一个电话就像... 响应 = new WebResourceResponse(mime, 字符集, isContents); ...不起作用。

感谢您提供任何有用的答案

Edited:

myRewritingHelper.getConnection(..) 的方法很快,它只是打开带有附加 http 标头的连接:

private HttpURLConnection getConnection(String url, String httpMethod) throws MalformedURLException, IOException {

        String absoluteRewrittenUrl = urlConfigurationManager.getRewritedUrl(url); // this gets a rewritten url

        final HttpURLConnection connection = (HttpURLConnection) new URL(absoluteRewrittenUrl).openConnection();
        connection.setRequestMethod(httpMethod);
        connection.setConnectTimeout(CONNECTION_TIMEOUT_MS);
        connection.setReadTimeout(SOCKET_TIMEOUT_MS);
        connection.setRequestProperty("AUTHORIZATION",getBasicAuthentication());

        return connection;
    }

getConnection(..) 方法仅消耗几毫秒。

shouldInterceptRequest 方法中最大的“瓶颈”是注释 // Read input 之后的 3 个调用

String charset = conn.getContentEncoding() != null
conn.getContentEncoding():Charset.defaultCharset().displayName();
String mime = conn.getContentType();
InputStream is = conn.getInputStream();

这 3 个调用每次最多消耗 2 秒。因此,shouldInterceptRequestMethod() 每次调用都会消耗超过 2 秒的时间。(这就是我要求异步调用此方法的原因)

米哈伊尔·纳加诺夫建议进行预取。任何人都可以展示如何预取数据并将数据正确提供给 WebResourceResponse 的示例吗?

如果我使用真正的 mime 类型而不是 null 创建 WebResourceResponse (请参阅 *2),则无法加载内容。 html/text 将在 WebView 中显示为文本。

编辑2:米哈伊尔建议的解决方案似乎是正确的。 但不幸的是事实并非如此:

public class MyWebResourceResponse extends WebResourceResponse {
    private String url;
    private Context context;
    private MyResourceDownloader myResourceDownloader;
    private String method;
    private Map<String, String> requestHeaders;
    private MyWebViewListener myWebViewListener;
    private String predefinedEncoding;

public MyWebResourceResponse(Context context, String url, MyResourceDownloader myResourceDownloader, String method, Map<String, String> requestHeaders, MyWebViewListener myWebViewListener,String predefinedEncoding) {
        super("", "", null);
        this.url = url;
        this.context = context;
        this.myResourceDownloader = myResourceDownloader;
        this.method = method;
        this.requestHeaders = requestHeaders;
        this.myWebViewListener = myWebViewListener;
        this.predefinedEncoding = predefinedEncoding;
    }

    @Override
    public InputStream getData() {
        return new MyWebResourceInputStream(context, url, myResourceDownloader, method, requestHeaders, myWebViewListener);
    }

    @Override
    public String getEncoding() {
        if(predefinedEncoding!=null){
            return predefinedEncoding;
        }
        return super.getEncoding();
    }

    @Override
    public String getMimeType() {
        return super.getMimeType();
    }
}

MyWebResourceInputStream 是这样的:

public class MyWebResourceInputStream extends InputStream {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyWebResourceInputStream.class);
    public static final int NO_MORE_DATA = -1;
    private String url;
    private boolean initialized;
    private InputStream inputStream;
    private MyResourceDownloader myResourceDownloader;
    private String method;
    private Map<String, String> requestHeaders;
    private Context context;
    private MyWebViewListener myWebViewListener;

public MyWebResourceInputStream(Context context, String url, MyResourceDownloader myResourceDownloader,
            String method, Map<String, String> requestHeaders, MyWebViewListener myWebViewListener) {
        this.url = url;
        this.initialized = false;
        this.myResourceDownloader = myResourceDownloader;
        this.method = method;
        this.requestHeaders = requestHeaders;
        this.context = context;
        this.myWebViewListener = myWebViewListener;
    }
@Override
    public int read() throws IOException {
        if (!initialized && !MyWebViewClient.getReceived401()) {
            LOGGER.debug("- -> read ENTER *****");
            try {
                InterceptingHelper.InterceptingHelperResult result = InterceptingHelper.getStream(context, myResourceDownloader, url, method, requestHeaders, false);
                inputStream = result.getInputstream();
                initialized = true;
            } catch (final UnexpectedStatusCodeException e) {
                LOGGER.warn("UnexpectedStatusCodeException", e);
                if (e.getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    MyWebViewClient.setReceived401(true);
                    if (myWebViewListener != null) {
                        myWebViewListener.onReceivedUnexpectedStatusCode(e.getStatusCode());
                    }
                    LOGGER.warn("UnexpectedStatusCodeException received 401", e);
                }
            } catch (IllegalStateException e) {
                LOGGER.warn("IllegalStateException", e);
            }
        }
        if (inputStream != null && !MyWebViewClient.getReceived401()) {
            return inputStream.read();
        } else {
            return NO_MORE_DATA;
        }

    }
@Override
    public void close() throws IOException {
        if (inputStream != null) {
            inputStream.close();
        }
    }
@Override
    public long skip(long byteCount) throws IOException {
        long skipped = 0;
        if (inputStream != null) {
            skipped = inputStream.skip(byteCount);
        }
        return skipped;
    }
@Override
    public synchronized void reset() throws IOException {
        if (inputStream != null) {
            inputStream.reset();
        }
    }
@Override
    public int read(byte[] buffer) throws IOException {
        if (inputStream != null) {
            return inputStream.read(buffer);
        }
        return super.read(buffer);
    }
@Override
    public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
        if (inputStream != null) {
            return inputStream.read(buffer, byteOffset, byteCount);
        }
        return super.read(buffer, byteOffset, byteCount);
    }
 public int available() throws IOException {
        if (inputStream != null) {
            return inputStream.available();
        }
        return super.available();
    }

public synchronized void mark(int readlimit) {
        if (inputStream != null) {
            inputStream.mark(readlimit);
        }
        super.mark(readlimit);
    }
 @Override
    public boolean markSupported() {
        if (inputStream != null) {
            return inputStream.markSupported();
        }
        return super.markSupported();
    }

呼叫发起于

MyWebViewClient extends WebViewClient{

    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request){
    // a lot of other code
    String predefinedEncoding = getPredefinedEncodingFromUrl(url);
            return new MyWebResourceResponse(context, url, myResourceDownloader, method, requestHeaders, webViewListener, predefinedEncoding);
  }
}

它带来了性能提升,但它有一个巨大的缺点,即在创建 MyWebResourceResponse 类期间未定义编码。因为直到调用 MyWebResourceInputStream.read() 后才会建立连接。 我发现,当未建立连接时,webkit 在 getData() 之前调用 getEncoding(),因此 getEncoding 始终为 null。 我开始使用预定义的编码定义解决方法(取决于 url )。但这距离通用解决方案还很遥远!并且并非在每种情况下都有效 有人知道替代解决方案吗?抱歉米哈伊尔拿走了已接受的答案。


资源加载过程包括两个阶段:创建请求作业,然后运行它们以获取数据。shouldInterceptRequest在第一阶段调用,这些调用确实按顺序在单个线程上运行。但是当 WebView 的资源加载器接收到请求作业时,它就会开始并行地从提供的流加载资源内容。

创建请求作业应该很快,并且不应该成为瓶颈。你是否真正测量过你需要多长时间shouldInterceptRequest去完成?

下一步是检查输入流实际上没有相互阻塞。另外,RewritingHelper 是否预取内容,或者仅在读取流时按需加载它们?预取有助于提高加载速度。

至于 mime 类型——通常浏览器从响应头中获取它,这就是为什么需要通过以下方式提供它:WebResourceResponse构造函数。我实际上不确定您的评论中的“WebResourceResponse 直接从流获取类型”是什么意思 - 流仅包含回复的数据,但不包含响应标头。

UPDATE

因此,从您更新的问题看来 HttpURLConnection 实际上确实如此loading里面的资源shouldInterceptRequest,这就是为什么一切都如此缓慢。您需要做的是定义自己的类来包装 WebResourceResponse 并且对构造不执行任何操作,因此shouldInterceptRequest执行速度快。实际加载应在之后开始。

我找不到这种技术的很多好的代码示例,但是这个示例似乎或多或少地满足了您的需要:https://github.com/mobilyzer/Mobilyzer/blob/master/Mobilyzer/src/com/mobilyzer/util/AndroidWebView.java#L252

我所说的预取是指您从以下位置返回后几乎可以立即开始加载数据:shouldInterceptRequest,不等到WebView调用getData返回的方法WebResourceResponse。这样,当 WebView 询问时,您就已经加载了数据。

UPDATE 2

这实际上是WebView中的一个问题,它在收到实例后立即查询响应头WebResourceResponse from shouldInterceptRequest。这意味着如果应用程序想要从网络本身加载资源(例如修改它们),加载速度永远不会像 WebView 本身加载这些资源时一样快。

应用程序可以做的最好的方法是这样的(代码缺乏适当的异常和错误处理,否则它会大3倍):

public WebResourceResponse shouldInterceptRequest(WebView view, final WebResourceRequest request) {
    final CountDownLatch haveHeaders = new CountDownLatch(1);
    final AtomicReference<Map<String, String>> headersRef = new AtomicReference<>();
    final CountDownLatch haveData = new CountDownLatch(1);
    final AtomicReference<InputStream> inputStreamRef = new AtomicReference<>();

    new Thread() {
        @Override
        public void run() {
            HttpURLConnection urlConnection =
                (HttpURLConnection) new URL(request.getUrl().toString()).openConnection();
            Map<String, List<String>> rawHeaders = urlConnection.getHeaderFields();
            // Copy headers from rawHeaders to headersRef
            haveHeaders.countDown();

            inputStreamRef.set(new BufferedInputStream(urlConnection.getInputStream()));
            haveData.countDown();
        }
    }.start();

    return new WebResourceResponse(
            null,
            "UTF-8",
            new InputStream() {
               @Override
               public int read() throws IOException {
                   haveInputStream.await(100, TimeUnit.SECONDS));
                   return inputStreamRef.get().read();
            }) {

        @Override
        public Map<String, String> getResponseHeaders() {
            haveHeaders.await(100, TimeUnit.SECONDS))
            return headersRef.get();
        }
    }
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何异步调用 WebViewClient.shouldInterceptRequest 的相关文章

  • Android:动态更改Listview中的图像

    我有一个由以下 xml 定义的列表视图 我需要切换图像当用户单击任何行时 在运行时会出现在列表中 我怎样才能实现这个目标 非常感谢任何帮助 谢谢 list item xml
  • 如何增加 Gradle 守护进程的最大堆大小?

    签署 apk 时 我收到以下消息 To run dex in process the Gradle daemon needs a larger heap It currently has 1024 MB For faster builds
  • 在 Android 上检测已接听的拨出电话

    我知道这个问题已经被问过很多次了 但没有答案 但我仍然希望有人终于解决了这个问题 问题 我有一台运行 Android 2 3 的未 root 设备 我需要创建一项服务 打电话 等待呼叫被应答 接听电话后挂断电话 有超时 和其他许多人一样 我
  • Android 测试 java.lang.NoClassDefFoundError 由于 Fest-Android 出现错误

    我目前正在我的项目中实现 Android 版 Fest 但我似乎遇到了依赖问题 如果我在不包含 Fest 库的情况下运行测试 测试将正常运行 一旦我添加了 Fest 库 测试就不再运行 相反 会抛出异常 我的项目使用以下依赖项 compil
  • 收到“提供的 API 密钥已过期”。使用 Places API 时出错

    我已经从 Google 控制台为 Places API 密钥生成了服务器密钥 但每当我访问该服务时 我都会收到 提供的 API 密钥已过期 错误 我已尝试重新生成密钥 但仍然出现相同的错误 我遇到了同样的问题 但终于解决了 Google 地
  • android新手需要了解“?android:attr/actionBarSize”

    我正在经历拉尔斯 沃格尔的教程 http www vogella com articles AndroidFragments article html在使用 Fragments 时 我遇到了以下代码 android layout margi
  • Android:NotificationCompat.MediaStyle 操作按钮不执行任何操作

    我有一个简单的 Android 应用程序 其中包含一个Activity and a Service源自于MediaBrowserServiceCompat 我已成功将其设置为通过使用播放我的主要活动中的音频MediaBrowserCompa
  • 按钮上方带有文本的单选按钮

    我是 Android 新手 我需要在我的活动中添加单选按钮 但我需要将文本放在项目符号按钮的顶部 请提供任何帮助 我发现了以下内容 尽管我不明白 drawable in 选择器和 style Tab 样式是什么 顶部带有文本的单选按钮 ht
  • 获取包含位图支持的画布的 Android 视图上的点的像素颜色值

    我正在尝试找出获取给定点上像素颜色值的最佳方法View http developer android com reference android view View html 我可以通过三种方式写入视图 我设置了背景图像View setBa
  • toArray 与预先确定大小的数组

    使用时ar toArray new String ar size 安卓工作室3 2 1警告预先确定大小的数组并建议空数组 有两种方式将集合转换为数组 使用 预先确定大小的数组 如 c toArray new String c size 或使
  • 错误:类 kotlin.reflect.jvm.internal.FunctionCaller$FieldSetter

    我已尝试一切方法来消除此错误 但它不断出现 Class kotlin reflect jvm internal FunctionCaller FieldSetter can not access a member of class com
  • android device.getUuids 返回 null

    我正在尝试使用低功耗蓝牙 BLE 通过 Android 应用程序连接到 Arduino Uno 我正在 Android Studio 上进行开发 使用 Samsung Galaxy S4 和 Android 版本 5 0 1 进行测试我点击
  • 可下载字体例外

    我决定使用可下载字体 https developer android com guide topics ui look and feel downloadable fonts html在我的项目中 IS 按照指南中的建议实施了所有内容 当我
  • Android Studio错误的含义:未注释的参数覆盖@NonNull参数

    我正在尝试 Android Studio 创建新项目并添加默认值后onSaveInstanceState方法创建 MyActivity 类 当我尝试将代码提交到 Git 时 我收到一个我不明白的奇怪错误 代码是这样的 我得到的错误是这样的
  • 使用Android Camera API,拍摄照片的方向始终未定义

    我使用相机API 拍摄的照片总是旋转90度 我想旋转它 所以首先我想知道图片的方向 这一点我被卡住了 我总是以两种方式得到未定义的方向 这是代码 Override public void onPictureTaken byte data C
  • 当应用程序从最近的应用程序中滑动时,前台服务会被终止,通知也会被删除

    我有一个foreground service有通知 当应用程序从最近的应用程序托盘中滑出时 服务将被终止 通知也会被删除 这是我的服务的代码VoiceService class Override public IBinder onBind
  • 如何以相同的意图从相机获取全尺寸图片和缩略图

    我一直需要找到这个问题的解决方案 我已经从这个社区搜索并测试了许多解决方案 但任何人都适合帮助我 我有两个活动 第一个活动拍摄一张照片并将其发送到另一个活动 该活动有一个 ImageView 来接收该照片 直到这里我遇到问题 以及一个在数据
  • 不幸的是 Project_Name 已停止

    我有一个简单的应用程序 您可以在文本视图中输入文本并按提交 它会在另一个活动中显示文本 然而 当我按下提交时 给我消息 不幸的是 发送已停止 我查看了SO上的其他线程 但是不幸的是 myfirstproject 在 java 中停止工作错误
  • 如何为部分 Android 活动创建通用代码?

    我的申请中有 14 项活动 这 9 个活动中包含自定义标题栏和选项卡窗格 所以在这里我需要在一个地方编写这个通用代码 而不是在每个包含自定义标题栏和选项卡窗格代码的活动中编写冗余代码 即布局及其活动特定代码 有哪些可能的方法可以做到这一点
  • 在自定义对话框中设置文本视图

    我创建了一个自定义对话框 但无法将文本设置到 java 对话框布局中的文本视图中 并且我的程序崩溃了 我的错误是什么 public class Total CBC extends Activity Override protected vo

随机推荐