31

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 = steve@steve.com in the form, I want to change the values to name = bob and email = bob@bob.com in the android app and have the new POST be sent to http://www.example.com/do.php.

Thanks for your help!

javajavajavajavajava
  • 1,223
  • 2
  • 12
  • 20
  • post your complete code then i can help you – surendra Sep 28 '11 at 14:20
  • Hi @surenda. I'd really appreciate your help if you know anything on this subject. I only have a basic webview right now that enables javascript and sets the webview client. I just need a proof of concept for my client so I can continue development. I was originally trying to create the post data myself and submit it using `postUrl()` but it wasn't working well. The plan of this project is to be able to work with any site that uses a POST form so I don't have any website code. If you still want to see my code I'll post it, but there's not much ATM. Thanks! – javajavajavajavajava Sep 28 '11 at 14:51
  • probably user presses some button after he enters his credentials, just put the code under onClickListener, get the data from EditText's, modify it and send a new post. Can do it in another thread,m so that the usual POST will be sent as well –  Sep 28 '11 at 15:24
  • That's a great idea... I think that's what I'll have to do something like that. I'm not sure how to set on onClickListener for a page in a webView (setting it on the webview doesn't work) and I've tried `shouldOverrideUrlLoading()` with no luck. If you could provide some more details in an answer that would be much appreciated, and I'll most likely post it as the accepted answer. Thanks! @MocialovBoris – javajavajavajavajava Sep 30 '11 at 16:30
  • there i have a small post how to operate with the HttpClient, it is quite simmilar with HttpConnection: http://stackoverflow.com/questions/6746375/java-httppost-into-asp-form –  Sep 30 '11 at 19:20
  • In the case when you want to intercept the UrlLoading(shouldOverrideUrlLoading()) then, in my case I have used like - if(url.contains("any substring from the URL")){ //Do something with the url} –  Sep 30 '11 at 19:21
  • Do not use OnClickListener, because you can only apply it to the WebView widget, but not to the content of the WebView. For the content use ShouldOverrideUrlLoading P.S. I will have access to my computer only on monday and then I can give you an example what I have been working on with the WebView –  Sep 30 '11 at 19:24
  • Hi @javajavajavajavajava any updates ? I find mysefl in the same spot . – Rick Apr 10 '12 at 13:50

6 Answers6

3

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.

CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
Jay
  • 1,077
  • 9
  • 9
  • I haven't worked on this project for a long time now (maybe 2 years) but I believe this would have worked. What I ended up doing was getting the user to enter their url, then I would pull the raw html from the server and parse it out to insert my custom code to change the way the form was submitted. I believe inserting custom javascript that I could hook onto in code would have worked wonders, but I never tried it out. Thanks for the answer! – javajavajavajavajava Mar 15 '13 at 14:08
3

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

Konstantin Schubert
  • 3,242
  • 1
  • 31
  • 46
2

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);
Sathesh
  • 6,323
  • 6
  • 36
  • 48
  • 2
    I think you misunderstood my question. I was not submitting the form in code, but rather the user would submit the form in the webview and I was trying to intercept the post data. The intercepting of the post data was where most of my issues lay. I've long ago passed on the project to another developer (long story short: cheap clients) so I can't really test if this works. I think what I ended up doing was intercepting the url the user entered then parsing the raw html and inserting my own code then displaying that in the webview. That allowed me to change the way the form was submitted. – javajavajavajavajava Mar 15 '13 at 14:04
-2

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

Community
  • 1
  • 1
Gene Bo
  • 11,284
  • 8
  • 90
  • 137
  • Where do you get the request from? The url only gives the get parameters – Sohaib Mar 31 '15 at 11:07
  • I actually just used this solution to handle UI updates after the form is submitted. I didn't do the part with extracting/modifying the POST params. This post here seems to address the situation for the POST params http://stackoverflow.com/a/21095771/2162226 . As it mentions, one option (though maybe not great) is to do your own web requests with the UI data – Gene Bo Mar 31 '15 at 16:49
  • Here's one more post that describes an approach for sending your own post data http://stackoverflow.com/a/6290178/2162226 . The code of interest: String postData = "username=my_username&password=my_password"; webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64")); .. and then in the onPageStarted() method .. I'm guessing you don't call the super impl. super.onPageStarted(view, url, favicon); .. will have to try to see – Gene Bo Mar 31 '15 at 16:56
  • Ah but you see that is submitting a post to a webview. What I talk of is capturing a POST request that the user just submitted through a webview. I researched a bit and found that this has been made possible since API Level 21 but older APIs are unsupported. I came up with a little javascript trick to get at a solution. – Sohaib Apr 02 '15 at 06:16
  • As mentioned in an answer by Unknown_Rebel `shouldInterceptRequest` works after API Level 21 http://developer.android.com/reference/android/webkit/WebViewClient.html – Sohaib Apr 02 '15 at 08:58
  • 1
    so, what was your solution to intercept the post? I already captured the request by using should interceptRequeset method, but I have NO access to the request body, what I really need. – narancs Jun 05 '17 at 19:23
-3

overriding onPageStarted callback in your WebView

A.Quiroga
  • 5,704
  • 6
  • 37
  • 58
-5

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