views:

162

answers:

2

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

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.

LatinSuD
How would I do that?
Steven1350
+2  A: 

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

TJF
I have a piece of code quite similar to this, but I have no idea how to relate this with a WebView. If it isnt too much trouble, could you post a snippet of a WebView using this piece of code? Thanks
Steven1350
Once you have the data, do you merely want to display the results in the `WebView`, or are you wanting the user to continue clicking links? I believe once you load data from a `String` you can't interact with it. Elaborate a little more. ;-)
TJF
All I really want to do is submit POST data do a page, and then display the resulting page to the user
Steven1350
I guess I should add that the user will click links once viewing the webview if that is what you are asking, but the webview is configured to handle that already
Steven1350
Solved it. Refer to top for my solultion. Marking your answer as correct as it does the bulk of the work
Steven1350
Just be wary of this caveat from the docs: `Content loaded through this mechanism does not have the ability to load content from the network.` -- I'm not sure if that means you won't be able to interact with it once it is loaded or if it won't load `<img>` tags, or what. Thx for the accept!
TJF