5

I have a WebView in my app and I load http://mysite.com/somepage.php

I know I can pass data using the GET method because that would just be right in the URL string. I was wondering how I could pass data to the page using the POST method. I tried searching but I cannot find anything specific. Thanks.

Ronnie
  • 11,138
  • 21
  • 78
  • 140

1 Answers1

8

There's a method called postUrl in WebView.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView webView = new WebView(this);
    setContentView(webView);

    String url = "http://mysite.com/somepage.php";
    String postData = "postvar=value";

    webView.postUrl(url, EncodingUtils.getBytes(postData, "base64"));
}

If base64 didn't work, try BASE64 alternatively.

Hope it will works fine.

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
  • you're the man! That works perfect. Much easier than http://stackoverflow.com/questions/3471581/android-webview-post and it works. What if I want to add more than one post value? – Ronnie Jun 02 '11 at 18:30
  • 2
    neither "base64" nor "BASE64" are character sets that are guaranteed to exist. what you should do is String postData = "name=" + URLEncoder.encode(value, "UTF-8"); And the use getBytes – raudi May 15 '15 at 11:21
  • 2
    encodingutils deprecated. do you have any other solutions – Lokesh Jan 06 '16 at 05:55
  • How to recive post data in webview? – Chris Harris Jul 24 '17 at 05:37