Android Webview POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3471581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:20:16  来源:igfitidea点击:

Android Webview POST

androidpostwebview

提问by Se?or Reginold Francis

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.

我试图完成一些非常简单的事情,但我没有找到关于此的好的文档。我有一个 webView,我需要在其中加载一个需要 POST 数据的页面。看起来像一个简单的过程,但我找不到在 webView 中显示结果的方法。

The process should be simple:

过程应该很简单:

query(with POST data) -> webserver -> HTML response -> WebView.

查询(带有 POST 数据)-> 网络服务器-> HTML 响应-> WebView。

I can submit data using a DefaultHttpClient, but this cannot be displayed in a WebView.

我可以使用 DefaultHttpClient 提交数据,但这无法在 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");
    }

采纳答案by TJF

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

我建议将此作为 AsyncTask 的一部分并在之后更新 WebView

回答by tarkeshwar

Two ways to load post response in webview:

在 webview 中加载 post 响应的两种方法:

  1. webview.loadData(): Like what you have posted in your solution. But "content loaded through this mechanism does not have the ability to load content from the network".

  2. webview.postUrl(): Use this if post response needs to load content from the network. (NOTE: only accessible from api-level 5, which means no android 1.6 or lower)

  1. webview.loadData():就像您在解决方案中发布的内容一样。但是“通过这种机制加载的内容不具备从网络加载内容的能力”。

  2. webview.postUrl():如果发布响应需要从网络加载内容,请使用此选项。(注意:只能从 api-level 5 访问,这意味着没有 android 1.6 或更低版本)



String postData = "username=my_username&password=my_password";
webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));

(source: http://www.anddev.org/other-coding-problems-f5/webview-posturl-postdata-t14239.html)

(来源:http: //www.anddev.org/other-coding-problems-f5/webview-posturl-postdata-t14239.html

回答by HenryChuang

I use webView.loadData() to do client post, and it will display content of url, my code :

我使用 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");      
}

use function webview_ClientPost() :

使用函数 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);

回答by LatinSuD

If you use a WebView from the start could it work?

如果您从一开始就使用 WebView,它可以工作吗?

A Webview with a html/js that does the POST, and naturally displays the result.

带有 html/js 的 Webview 执行 POST,并自然地显示结果。