How to intercept POST data in an android webview

后端 未结 6 1082
故里飘歌
故里飘歌 2020-12-08 14:34

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

相关标签:
6条回答
  • 2020-12-08 14:46

    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);
    
    0 讨论(0)
  • 2020-12-08 14:49

    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

    0 讨论(0)
  • 2020-12-08 14:50

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

    0 讨论(0)
  • 2020-12-08 14:51

    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.

    0 讨论(0)
  • 2020-12-08 14:52

    overriding onPageStarted callback in your WebView

    0 讨论(0)
  • 2020-12-08 15:01

    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 :)

    0 讨论(0)
提交回复
热议问题