Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send POST data with code in an android webview

I have an android application that consists of a WebWiew and I need to login to a site automatically using code. I've tried using postUrl() and it seems to work... but only on some sites.

Here's the code I'm using:

public class webviewActivity extends Activity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         WebView webview = new WebView(this);         setContentView(webview);         WebSettings webSettings = webview.getSettings();         webSettings.setJavaScriptEnabled(true);          webview.setWebViewClient(new WebViewClient());          String postData = "[email protected]&login_password=myPassword";         webview.postUrl("https://www.dropbox.com/login", EncodingUtils.getBytes(postData, "utf-8"));     } } 

This Works great for dropbox.com, but other sites like google.com, facebook.com, etc. just load the login page or give an error (google.com gives an error saying I need to enable cookies).

Right now I'm just going the post data by hand; looking at the login form for the site and putting the name/value fields in the postData in my code. On sites like google, the login form has many hidden fields and I've been adding those to the postData also.

If anyone could give me any idea of something I'm doing wrong please let me know, I'm pretty confused about this.

like image 921
javajavajavajavajava Avatar asked Sep 28 '11 16:09

javajavajavajavajava


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 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.

Which method is used to load HTML content in WebView?

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.

How do you communicate between WebView and native Android?

2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.


2 Answers

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

like image 93
an00b Avatar answered Sep 28 '22 05:09

an00b


public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      WebView webView = new WebView(this);      setContentView(webView);      String url = "http://example.com/somepage.php";     String postData = "postvar=value&postvar2=value2";      webView.postUrl(url, EncodingUtils.getBytes(postData, "base64")); } 
like image 36
Laxman Avatar answered Sep 28 '22 05:09

Laxman