Android Webview POST [英] Android Webview POST
问题描述
我试图完成一些非常简单的事情,但我没有找到关于此的好的文档.我有一个 webView,我需要在其中加载一个需要 POST 数据的页面.看起来是一个简单的过程,但我找不到在 webView 中显示结果的方法.
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.
过程应该很简单:
query(with POST data) -> webserver -> HTML response -> WebView.
query(with POST data) -> webserver -> HTML response -> WebView.
我可以使用 DefaultHttpClient 提交数据,但无法在 WebView 中显示.
I can submit data using a DefaultHttpClient, but this cannot be displayed in a 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");
}
推荐答案
试试这个:
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);
}
我建议将此作为 AsyncTask 的一部分并在之后更新 WebView
I would recommend doing this as part of an AsyncTask and updating the WebView afterwards
这篇关于Android Webview POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!