Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post data for WebView android

I need to post data to Webview.
I found from some of the links the below code:

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

But in my android studio I see EncodingUtils as deprecated
Can anyone help me what is the alternative for EncodingUtils to post data to Android WebView?

like image 382
Android_programmer_office Avatar asked Sep 15 '16 08:09

Android_programmer_office


People also ask

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 Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.

What is loadUrl?

loadUrl when you say loadUrl. From documentation, only difference between them is, loadURL renders a webkit having a url that you set. On the other hand, loadData renders webkit, that source code comes from a parameter, and baseURL is also a parameter.


1 Answers

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()) 
like image 63
Priyank Patel Avatar answered Oct 13 '22 00:10

Priyank Patel