【发布时间】:2011-03-29 03:31:11
【问题描述】:
我正在尝试完成一些非常简单的事情,但我没有找到关于此的好的文档。我有一个 webView,我需要在其中加载一个需要 POST 数据的页面。似乎是一个简单的过程,但我找不到在 webView 中显示结果的方法。
过程应该很简单:
查询(带有 POST 数据)-> 网络服务器 -> HTML 响应 -> WebView。
我可以使用 DefaultHttpClient 提交数据,但这不能在 WebView 中显示。
有什么建议吗?
非常感谢
解决方案
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");
}
【问题讨论】: