Android中的WebView之loadDataWithBaseURL()与loadData()的差别

一、参数差别

this.web.loadData(html, "text/html", "utf-8");
   

this.web.loadDataWithBaseURL("about:blank", html, "text/html", "utf-8", null);

二、loadData需要先转码。

loadData不支持#、%、\、? 四种字符。但也不是完全不支持,表现很怪异。

最好的办法,是用以下方法转码:

 

  final String digits = "0123456789ABCDEF";    
  public String encode(String s)


                  // Guess a bit bigger for encoded form  
                  StringBuilder buf = new StringBuilder(s.length() + 16); 
                  for (int i = 0; i < s.length(); i++) { 
                      char ch = s.charAt(i); 
                      if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 
                              || (ch >= '0' && ch <= '9') || ".-*_".indexOf(ch) > -1) { //$NON-NLS-1$  
                          buf.append(ch); 
                      } else { 
                          byte[] bytes = new String(new char[] { ch }).getBytes(); 
                          for (int j = 0; j < bytes.length; j++) { 
                              buf.append('%'); 
                              buf.append(digits.charAt((bytes[j] & 0xf0) >> 4)); 
                              buf.append(digits.charAt(bytes[j] & 0xf)); 
                          } 
                      } 
                  } 
                  return buf.toString(); 
      }

 

即:

this.web.loadData(this.encode(html), "text/html", "utf-8");

 

此方法来自 http://blog.csdn.net/feng88724/article/details/6654471

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页