Android Webview Post

How to post data for WebView android

Try like below...

Java:

 WebView webview = new WebView(this);
setContentView(webview);

String url = "http://www.example.com";

String postData = "username=" + URLEncoder.encode(my_username, "UTF-8") + "&password=" + URLEncoder.encode(my_password, "UTF-8");
webview.postUrl(url,postData.getBytes());

Kotlin:

val webview = WebView(this)
setContentView(webview)

val url = "http://www.example.com"

val postData = "username=${URLEncoder.encode(my_username, "UTF-8")}" +
"&password=${URLEncoder.encode(my_password, "UTF-8")}"
webview.postUrl(url, postData.toByteArray())

Android Webview POST request

In Android you do not use webView to access the content of the HTTP response. You'll need to use HttpClient for that purpose!

See this nice tutorial which explains the fundamentals! Also see this video if you find it hard!

Hope it helps!

Post data to webview in android

Try this

String url = "http://www.example.com";
String postData = "username=my_username&password=my_password";
webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));

Android Webview POST

Try this:

private static final String URL_STRING = "http://www.yoursite.com/postreceiver";

public void postData() throws IOException, ClientProtocolException {

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("foo", "12345"));
nameValuePairs.add(new BasicNameValuePair("bar", "23456"));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL_STRING);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

}

I would recommend doing this as part of an AsyncTask and updating the WebView afterwards

How to send POST data with code in an android webview

Try replacing "utf-8" (in the 2nd param) with "BASE64".



Related Topics



Leave a reply



Submit