Señ*_*cis 43 post android webview
我试图完成一些非常简单的事情,但我没有找到关于此的好文档.我有一个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 {
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");
}
Run Code Online (Sandbox Code Playgroud)
tar*_*war 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"));
Run Code Online (Sandbox Code Playgroud)
(来源:http://www.anddev.org/other-coding-problems-f5/webview-posturl-postdata-t14239.html)
小智 16
试试这个:
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);
}
Run Code Online (Sandbox Code Playgroud)
我建议将此作为AsyncTask的一部分,然后更新WebView
Hen*_*ang 11
我使用webView.loadData()来做客户端帖子,它会显示url的内容,我的代码:
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");
}
Run Code Online (Sandbox Code Playgroud)
使用函数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);
Run Code Online (Sandbox Code Playgroud)