Android - 如何在 API 级别 4 上拦截 android WebViewClient 中的表单 POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3664440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:15:02  来源:igfitidea点击:

Android - how to intercept a form POST in android WebViewClient on API level 4

androidformspostwebviewclient

提问by manisha

I have a WebViewClientattached to my WebViewlike so:

我有一个像这样的WebViewClient附件WebView

webView.setWebViewClient(new MyWebViewClient());

Here is my implementation of MyWebViewClient:

这是我的实现MyWebViewClient

private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      webView.loadUrl(url);
      return true;
    }    
}

I give the WebViewa URL to load via loadUrl(). If I have a link (a href...) in the page, my shouldOverrideUrlLoadingmethod is called and I can intercept the link click.

我给了WebView一个 URL 以通过loadUrl(). 如果我a href...在页面中有一个链接 ( ),我的shouldOverrideUrlLoading方法就会被调用,我可以拦截链接点击。

However, if I have a form whose method is POST, the shouldOverrideUrlLoadingmethod is not called.

但是,如果我有一个方法为 的表单,则不会调用POSTshouldOverrideUrlLoading方法。

I noticed a similar issue here: http://code.google.com/p/android/issues/detail?id=9122which seems to suggest overriding postUrlin my WebView. However, this API is only available starting from API level 5.

我在这里注意到一个类似的问题:http: //code.google.com/p/android/issues/detail?id=9122这似乎建议postUrl在我的WebView. 但是,此 API 仅从 API 级别 5 开始可用。

What can I do if I'm on API level 4? Is there any other way to intercept form posts?

如果我处于 API 级别 4,我该怎么办?有没有其他方法可以拦截表单帖子?

采纳答案by Mike Keskinov

This is known issue, that shouldOverrideUrlLoadingdon't catch POST. See http://code.google.com/p/android/issues/detail?id=9122for details.

这是已知问题,shouldOverrideUrlLoading不会捕获 POST。有关详细信息,请参阅http://code.google.com/p/android/issues/detail?id=9122

Use GET! I personally tried using POST, because I expected some limitation of GET parameters (i.e. length of URL), but I just successfully passed 32000 bytes through GET locally without any problems.

使用获取!我个人尝试使用 POST,因为我预计 GET 参数(即 URL 长度)有一些限制,但我只是成功地通过本地 GET 传递了 32000 个字节,没有任何问题。

回答by Ivo van der Wijk

Do you really need to use a POST? If you want to handle formdata locally, why not have a piece of javascript handle your form and interface with "native" java code using addJavascriptInterface. E.g.

你真的需要使用POST吗?如果您想在本地处理表单数据,为什么不让一段 javascript 使用 addJavascriptInterface 来处理您的表单和带有“本机”java 代码的界面。例如

WebView engine = (WebView) findViewById(R.id.web_engine);       
engine.getSettings().setJavaScriptEnabled(true); 
engine.addJavascriptInterface(new MyBridge(this), "bridge");
engine.loadUrl(...)

Your bridge can be any class basically and you should be able to access its methods directly from javascript. E.g.

您的网桥基本上可以是任何类,您应该能够直接从 javascript 访问其方法。例如

public class MyBridge {

    public MyBridge(Context context) {
         // ...
    }

    public String doIt(String a, String b) {
            JSONArray result = new JSONArray();
            result.put("Hello " + a);
            result.put("Hello " + b);
            return result.toString();       
    }

Your html / javascript could look like:

您的 html/javascript 可能如下所示:

<script type="text/javascript">
    $("#button").click(function() {
        var a = $("#a").val();
        var b = $("#b").val();

        var result=JSON.parse(bridge.doIt(a, b));
        // ...
    }
</script>

<input id="a"><input id="b"><button id="button">click</button>

回答by wangzhengyi

I think you can override onLoadResource(WebView view, String url)from WebViewClient. This function is Added in API LEVEL 1.

我认为您可以onLoadResource(WebView view, String url)WebViewClient. 此功能是在 API LEVEL 1 中添加的。

This function is called when WebViewwill load the resource specified by the given url. Resource include js, css, iframeembeded url. Code example like this:

WebView将加载给定 url 指定的资源时调用此函数。资源包括js, css,iframe嵌入的 url。代码示例如下:

    @Override
    public void onLoadResource(WebView view, String url) {
        if (url.indexOf("http://www.example.com") != -1 && view != null) {
            // open url in default browser
            view.stopLoading();
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    }