Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept POST data in an android webview

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!

like image 818
javajavajavajavajava Avatar asked Sep 28 '11 14:09

javajavajavajavajava


People also ask

How do you intercept a URL in WebView?

Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead. if the tap in Webview is a link to custom URL, (like myurl://parameters), its not getting called.

How do I send a post request in WebView?

webView. setWebViewClient(new WebViewClient(){ public void onPageStarted(WebView view, String url, Bitmap favicon) { super. onPageStarted(view, url, favicon); } public boolean shouldOverrideUrlLoading(WebView view, String url) { webView. postUrl(Base_Url, postData.

Is it possible to get data from HTML forms into Android while WebView?

Yes you can, you can use javascript to get webpage content. Then use the webview jsInterface to return the content to you java code.


3 Answers

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.

like image 196
Jay Avatar answered Oct 01 '22 01:10

Jay


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

like image 44
Konstantin Schubert Avatar answered Oct 01 '22 02:10

Konstantin Schubert


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);
like image 29
Sathesh Avatar answered Oct 01 '22 01:10

Sathesh