【问题标题】:Why WebResourceResponse return null in shouldInterceptRequest()?为什么 WebResourceResponse 在 shouldInterceptRequest() 中返回 null?
【发布时间】:2016-08-04 05:33:20
【问题描述】:

我正在尝试在 WebView 中获取请求的 POST 响应。这是我的代码。

mWebview.setWebViewClient(new WebViewClient() {

    @Override
    public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
        WebResourceResponse w = super.shouldInterceptRequest(view, url);
        Log.i("type", w.getMimeType());
        return super.shouldInterceptRequest(view, url);
    }
});

但是程序崩溃了,错误是 w 为空。谁能告诉我为什么会这样或给我任何解决方案以在 WebView 中获取请求的 POST 响应?

谢谢。

【问题讨论】:

  • 你有没有为此找到解决方案?
  • 没关系,找到答案了。

标签: android http post webview response


【解决方案1】:

shouldInterceptRequest() return null,表示没有人拦截请求,webview会加载origin URL。

如果你想拦截请求,你应该使用 URL 参数中的信息创建自己的WebResourceResponse

【讨论】:

    【解决方案2】:

    在文档中,它声明“如果返回值为 null,WebView 将照常继续加载资源。”。如果某些东西应该拦截请求,则响应由 override 方法返回。

    https://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest(android.webkit.WebView, android.webkit.WebResourceRequest)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-17
      • 2014-06-19
      • 2022-12-26
      • 2016-12-28
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多