Android Webview POST

I am trying to accomplish something rather simple, but I have not found any good documentation on this. I have a webView, and I need to load a page into it that requires POST data. Sounds like a simple process, but I can't find a way to display the result in a webView.

The process should be simple:

request (with POST data) -> web server -> HTML response -> WebView.

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

Any suggestions?

Thank you very much

Decision

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"); } 
+42
android post webview
Aug 12 '10 at 20:03
source share
4 answers

Try the following:

 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 AsyncTask and updating WebView afterwards

+15
Aug 12 '10 at 21:08
source share

Two ways to upload a post response to webview:

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

  • webview.postUrl() : use this if post response needs to download content from the web. (NOTE: only available from api level 5, which means there is 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 )

+141
Jun 09 2018-11-11T00:
source share

I use webView.loadData () to send a client message, and it displays the contents of the url, my code is:

 public static void webview_ClientPost(WebView webView, String url, Collection< Map.Entry<String, String>> postData){ StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); webView.loadData(sb.toString(), "text/html", "UTF-8"); } 

use the webview_ClientPost () function:

 Map<String, String> mapParams = new HashMap<String, String>(); mapParams.put("param1", "111"); mapParams.put("param2", "222"); Collection<Map.Entry<String, String>> postData = mapParams.entrySet(); webview_ClientPost(webView1, "http://www.yoursite.com/postreceiver", postData); 
+11
May 28 '15 at 9:10
source share

If you use WebView from the start, can it work?

Web browsing with html / js that does POST and naturally displays the result.

+2
Aug 12 2018-10-12T00:
source share



All Articles