如何在android webview中拦截POST数据

发布于 2024-12-07 03:57:04 字数 840 浏览 0 评论 0原文

我有一个包含 webview 的 Android 应用程序。它需要允许用户在网页上填写表单,然后在用户单击表单上的提交后更改表单的数据。该表单将使用 POST 请求方法。

所以我的问题是,如何从表单中拦截 POST 数据,更改其值,然后发送它?

例如:如果有这样的 Web 表单...

<form action="http://www.example.com/do.php" method="post">
    <input type="text" name="name" />
    <input type="text" name="email" />
    <input type="submit" />
</form>

如果用户输入姓名= 史蒂夫和电子邮件 = [email protected] 在表单中,我想将值更改为 name = bob 和 email = [email protected] 并将新的 POST 发送到 http://www.example.com/do.php

感谢您的帮助!

I have an android app that consists of a webview. It needs to allow users to fill in a form on a webpage and then change the data of the form after the user has clicked submit on the form. The form will use the POST request method.

So my question is, how can I intercept the POST data from the form, change it's values, then send it along?

For example: If there's a web form like this...

<form action="http://www.example.com/do.php" method="post">
    <input type="text" name="name" />
    <input type="text" name="email" />
    <input type="submit" />
</form>

If the user enters name = Steve and email = [email protected] in the form, I want to change the values to name = bob and email = [email protected] in the android app and have the new POST be sent to http://www.example.com/do.php.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

带上头具痛哭 2024-12-14 03:57:04

如果你熟悉 JavaScript,我建议你使用 JavaScript。我认为这样更方便、更容易。 本教程将告诉您如何在 WebView 中使用 JavaScript。

If you are familiar with JavaScript, I would suggest you use JavaScript. I think it's more convenient and easy. This tour tells you how to use JavaScript in a WebView.

你好,陌生人 2024-12-14 03:57:04

如果您使用 postUrl() 方法提交表单,那么您可以像这样覆盖 WebView 对象中的 postUrl 方法。

 WebView mWebView = new WebView(this){

        @Override
        public void  postUrl(String  url, byte[] postData)
        {
            System.out.println("postUrl can modified here:" +url);
            super.postUrl(url, postData);
        }};
 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
 linearLayout.addView(mWebView);

if you are submitting the form using postUrl() method then you can override the postUrl method in your WebView object like this.

 WebView mWebView = new WebView(this){

        @Override
        public void  postUrl(String  url, byte[] postData)
        {
            System.out.println("postUrl can modified here:" +url);
            super.postUrl(url, postData);
        }};
 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
 linearLayout.addView(mWebView);
战皆罪 2024-12-14 03:57:04

我编写了一个带有特殊 WebViewClient 的库,它提供了修改后的shouldOverrideUrlLoading,您可以在其中访问发布数据。

https://github.com/KonstantinSchubert/request_data_webviewclient

不幸的是,我的实现适用于 XMLHttpRequest (AJAX) 请求仅有的。但其他人已经起草了如何对表单数据执行此操作: https://github.com/ KeejOow/android-post-webview

在这两个存储库之间,您应该能够找到答案:)

I wrote a library with a special WebViewClient that offers a modified shouldOverrideUrlLoading where you can have access to post data.

https://github.com/KonstantinSchubert/request_data_webviewclient

Unfortunately, my implementation works for XMLHttpRequest (AJAX) requests only. But somebody else drafted already how this would be done for form data: https://github.com/KeejOow/android-post-webview

Between the two repositories, you should be able to find your answer :)

嘿嘿嘿 2024-12-14 03:57:04

我喜欢 public void postUrl(String url, byte[] postData) 的建议,但不幸的是它对我不起作用。

我的解决方案只是拦截 POST 请求:

  • 有一个 WebViewClient 子类,该子类设置为 WebView
  • 覆盖 public void onPageStarted(WebView view, String url, Bitmap favicon) 来检查请求数据并采取相应行动(根据要求)

代码摘录&amp;其他想法:https://stackoverflow.com/a/9493323/2162226

I liked the suggestion for public void postUrl(String url, byte[] postData) , but unfortunately it did not work for me.

My solution for just intercepting the POST request:

  • have a WebViewClient subclass that is set for WebView
  • override public void onPageStarted(WebView view, String url, Bitmap favicon) to examine the request data and act accordingly (as per requirements)

Code excerpt & additional thoughts here: https://stackoverflow.com/a/9493323/2162226

苍暮颜 2024-12-14 03:57:04

覆盖 WebView 中的 onPageStarted 回调

overriding onPageStarted callback in your WebView

云柯 2024-12-14 03:57:04

我认为您可以使用 WebViewClient 的 shouldInterceptRequest(WebView view, String url) 方法,但 API 11 之后才支持它。

I think you can use shouldInterceptRequest(WebView view, String url) method of WebViewClient, but it is supported in the API's later than 11.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文