【问题标题】:Android Webview POSTAndroid Webview POST
【发布时间】:2011-03-29 03:31:11
【问题描述】:

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

过程应该很简单:

查询(带有 POST 数据)-> 网络服务器 -> HTML 响应 -> 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");
    }

【问题讨论】:

    标签: android post webview


    【解决方案1】:

    在webview中加载post response的两种方式:

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

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


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

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

    【讨论】:

    • 我认为这是更好的方法。但是上传图像文件怎么样?用这种方法能做到吗?
    • @tarkeshwar 难以置信。 SO 充满了冗长的 cumbersome 半生不熟的解决方案,需要 separate cookie 处理和管理,而你来了,发布了一个仅用 2 行即可完成所有工作的解决方案。我会投票给你+100,但SO只允许+1。恕我直言,这应该是公认的答案。感谢您为我节省了很多时间!
    • 在我看来这个问题的最佳答案
    • String postData 是 base64 编码,还是我们不能只使用 String.getBytes() 来获取 byte[]
    • 我认为 EncodingUtils 来自 Apache Commons,但是 Android 包含 android.utils.Base64 可以执行相同的任务:Base64.encode(params.getBytes(), Base64.DEFAULT);
    【解决方案2】:

    试试这个:

    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

    【讨论】:

    • 我有一段与此非常相似的代码,但我不知道如何将其与 WebView 关联起来。如果不是太麻烦,您可以使用这段代码发布 WebView 的 sn-p 吗?谢谢
    • 获得数据后,您是只想在WebView 中显示结果,还是希望用户继续点击链接?我相信一旦您从String 加载数据,您就无法与之交互。再详细一点。 ;-)
    • 我真正想做的就是提交 POST 数据做一个页面,然后将结果页面显示给用户
    • 我想我应该补充一点,如果这是您所要求的,用户将在查看 webview 后单击链接,但 webview 已配置为处理该问题
    • 解决了。请参阅顶部以获取我的解决方案。将您的答案标记为正确,因为它完成了大部分工作
    【解决方案3】:

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

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

    【讨论】:

    • 'org.apache.http.util.EncodingUtils' 已被弃用,所以这是最好的解决方案
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      您还可以将 JSON 格式的 post key:value 对传递给 Web 视图。

      String userName = "admin";
      String password = "Admin@2021"; 
          JSONObject jsonObject = new JSONObject();
              try {
                  jsonObject.put("userName", userName);
                  jsonObject.put("password", password);
              } catch (JSONException e) {
                  e.printStackTrace();
              }
      
      String url = "http://www.example.com";
      webView.postUrl(url, jsonObject.toString().getBytes());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多