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");
    }

Answers


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


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)


I use webView.loadData() to do client post, and it will display content of url, my code :

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 function webview_ClientPost() :

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);

If you use a WebView from the start could it work?

A Webview with a html/js that does the POST, and naturally displays the result.


Need Your Help

xmpp4r throws exception on login: "Exception caught in Parser thread! (Jabber::ServerDisconnected)"

ruby xmpp xmpp4r

I tried to connect to my XMPP server using xmpp4r in ruby and my code is very simple, but I don't know why I got error.

Mule ESB - Retrieve URL HTTP Request from file

http mule esb raml

I'm using Mule since 1 week. I already managed to connect to a HTTP REST request, using Quartz to trigger the events every tot minutes. My request gives me back an XML or a JSON. Now, I need to do ...