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

Solution 1:

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)

Solution 2:

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

Solution 3:

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

Solution 4:

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.

Solution 5:

You can also pass post key:value pair in JSON format to web view.

String userName = "admin";
String password = "Admin@2021"; 
    JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userName", userName);
            jsonObject.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }

String url = "http://www.example.com";
webView.postUrl(url, jsonObject.toString().getBytes());