问题描述
我想实现的东西很简单,但我发现这个没有好的文档。我有一个web视图,我需要加载一个页面就需要POST数据。似乎是一个简单的过程,但我不能找到一种方法,结果显示在一个web视图。
这个过程应该是简单的:
查询(用POST数据) - > Web服务器 - > HTML响应 - > web视图
余可使用DefaultHttpClient提交数据,但是这不能被显示在一个web视图。
有什么建议?
许多感谢
解决方案
私有静态最后弦乐URL_STRING =http://www.yoursite.com/postreceiver;
公共无效POSTDATA()抛出IOException异常,ClientProtocolException {
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(富,12345));
nameValuePairs.add(新BasicNameValuePair(酒吧,23456));
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(URL_STRING);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
。字符串数据=新BasicResponseHandler()用handleResponse(响应);
mWebView.loadData(数据,text / html的,UTF-8);
}
试试这个:
私有静态最后弦乐URL_STRING =http://www.yoursite.com/postreceiver;
公共无效POSTDATA()抛出IOException异常,ClientProtocolException {
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(富,12345));
nameValuePairs.add(新BasicNameValuePair(酒吧,23456));
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(URL_STRING);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
}
我会建议这样做,为的AsyncTask的一部分,之后更新的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.
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");
}
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
这篇关于Android的web视图POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!