Android Webview POST

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中加载帖子回复的两种方法:

  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"));
Run Code Online (Sandbox Code Playgroud)

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

  • @tarkeshwar令人难以置信.所以需要[单独](http://dackoverflow.com/questions/1652850/android-webview-cookie-problem/2349340#2349340)需要[单独](http:// stackoverflow). com/questions/5716898/set-a-cookie-to-a-webview-in-android/5717294#5717294)cookie处理和管理,在这里,你发布了一个只用2行完成所有操作的解决方案.我会投票给你+100但是只允许+1.恕我直言,这应该是公认的答案.谢谢你为我节省了很多时间! (8认同)
  • `String postData`****是否为base64编码,或者我们不能只使用`String.getBytes()`来获取`byte []`? (8认同)
  • 我认为`EncodingUtils`来自Apache Commons,但Android包含`android.utils.Base64`,它可以执行相同的任务:`Base64.encode(params.getBytes(),Base64.DEFAULT);` (4认同)
  • 优雅而优雅的答案. (2认同)

小智 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)