我试图完成一些非常简单的事情,但我没有找到关于此的好文档.我有一个webView,我需要加载一个需要POST数据的页面.看起来像一个简单的过程,但我找不到在webView中显示结果的方法.
这个过程应该很简单:
查询(使用POST数据) - > webserver - > HTML response - > WebView.
我可以使用DefaultHttpClient提交数据,但这不能在WebView中显示.
有什么建议?
非常感谢
解
private static final String URL_STRING = "http://www.yoursite.com/postreceiver"; public void postData() throws IOException, ClientProtocolException { ListnameValuePairs = new ArrayList (); 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"); }
tarkeshwar.. 143
在webview中加载帖子回复的两种方法:
webview.loadData()
:就像您在解决方案中发布的内容一样.但"通过此机制加载的内容无法从网络加载内容".
webview.postUrl()
:如果帖子响应需要从网络加载内容,请使用此选项.(注意:只能从api-level 5访问,这意味着没有android 1.6或更低版本)
String postData = "username=my_username&password=my_password"; webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));
(来源:http://www.anddev.org/other-coding-problems-f5/webview-posturl-postdata-t14239.html)
在webview中加载帖子回复的两种方法:
webview.loadData()
:就像您在解决方案中发布的内容一样.但"通过此机制加载的内容无法从网络加载内容".
webview.postUrl()
:如果帖子响应需要从网络加载内容,请使用此选项.(注意:只能从api-level 5访问,这意味着没有android 1.6或更低版本)
String postData = "username=my_username&password=my_password"; webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));
(来源:http://www.anddev.org/other-coding-problems-f5/webview-posturl-postdata-t14239.html)
试试这个:
private static final String URL_STRING = "http://www.yoursite.com/postreceiver"; public void postData() throws IOException, ClientProtocolException { ListnameValuePairs = new ArrayList (); 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); }
我建议将此作为AsyncTask的一部分,然后更新WebView
我使用webView.loadData()来做客户端帖子,它会显示url的内容,我的代码:
public static void webview_ClientPost(WebView webView, String url, Collection< Map.Entry> postData){ StringBuilder sb = new StringBuilder(); sb.append(""); sb.append(""); sb.append(String.format(""); webView.loadData(sb.toString(), "text/html", "UTF-8"); }
使用函数webview_ClientPost():
MapmapParams = new HashMap (); mapParams.put("param1", "111"); mapParams.put("param2", "222"); Collection > postData = mapParams.entrySet(); webview_ClientPost(webView1, "http://www.yoursite.com/postreceiver", postData);