Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Webview POST

I am trying to accomplish something quite simple, yet I have found no good documentation on this. I have a webView, and I need to load a page in it that requires POST data. Seems like a simple process, yet I cannot find a way to display the result in a webView.

The process should be simple:

query(with POST data) -> webserver -> HTML response -> WebView.

I can submit data using a DefaultHttpClient, but this cannot be displayed in a WebView.

Any suggestions?

Much Thanks

Solution

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);            String data = new BasicResponseHandler().handleResponse(response);          mWebView.loadData(data, "text/html", "utf-8");     } 
like image 224
Señor Reginold Francis Avatar asked Aug 12 '10 20:08

Señor Reginold Francis


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 WebViewClient?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.

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

Two ways to load post response in webview:

  1. webview.loadData(): Like what you have posted in your solution. But "content loaded through this mechanism does not have the ability to load content from the network".

  2. webview.postUrl(): Use this if post response needs to load content from the network. (NOTE: only accessible from api-level 5, which means no android 1.6 or lower)


String postData = "username=my_username&password=my_password"; webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64")); 

(source: http://www.anddev.org/other-coding-problems-f5/webview-posturl-postdata-t14239.html)

like image 145
tarkeshwar Avatar answered Oct 08 '22 16:10

tarkeshwar