今天遇到一个奇怪的问题。

我使用WebView加载一个网页。 

方法1. 直接使用 loadUrl() 方法,没有问题。完全可以。
方法2. 使用loadData()方法,出现问题,无法显示。
方法3. 使用loadDataWithBaseURL()方法, 完全可以。

--------------------------------------------------------------------------------------------
我就纳闷了,为什么唯独 webView.loadData()这个方法出错呢?

要知道,WebView是无法捕捉到401、404这样的错误的,只能捕捉到网络超时等这些错误。 所以为了可以判断服务器的相应,我需要HttpConnection来判断返回码, 先从网页上将数据拉下来,然后再通过loadData显示。 恩,不错的想法.. (当然用loadDataWithBaseURL也可以实现了,但是我还是想知道为什么loadData不可以。)

可是.. 现实有点残酷... 显示不了,NND!

看了下官方文档..

public void loadData (String data, String mimeType, String encoding)

Since: API Level 1
Load the given data into the WebView. This will load the data into WebView using the data: scheme. Content loaded through this mechanism does not have the ability to load content from the network.
Parameters data A String of data in the given encoding. The date must be URI-escaped -- '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.
mimeType The MIMEType of the data. i.e. text/html, image/jpeg
encoding The encoding of the data. i.e. utf-8, base64
public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl) Since: API Level 1
Load the given data into the WebView, use the provided URL as the base URL for the content. The base URL is the URL that represents the page that is loaded through this interface. As such, it is used to resolve any relative URLs. The historyUrl is used for the history entry.
Note for post 1.0. Due to the change in the WebKit, the access to asset files through "file:///android_asset/" for the sub resources is more restricted. If you provide null or empty string as baseUrl, you won't be able to access asset files. If the baseUrl is anything other than http(s)/ftp(s)/about/javascript as scheme, you can access asset files for sub resources.
Parameters baseUrl Url to resolve relative paths with, if null defaults to "about:blank"
data A String of data in the given encoding.
mimeType The MIMEType of the data. i.e. text/html. If null, defaults to "text/html"
encoding The encoding of the data. i.e. utf-8, us-ascii
historyUrl URL to use as the history entry. Can be null.

终于发现区别了。 原来loadData的第一个参数中,不能有特殊字符“#”,“%”,“\”, “?”. 必须将其转化为%23,%25,%27,%3f. 

'#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.  

而loadDataWithBaseURL 却没这个要求。

我了个去,这是何苦呢? 为什么要这样设计呢?

下面解决一下这个问题。 用encode方法转一下即可。

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();
}

本文转自:http://blog.csdn.net/feng88724/article/details/6654471

WebView loadData出错(奇怪的设计)的更多相关文章

  1. REST API出错响应的设计

    REST API应用很多,一方面提供公共API的平台越来越多,比如微博.微信等:一方面移动应用盛行,为Web端.Android端.IOS端.PC端,搭建一个统一的后台,以REST API的形式提供服务 ...

  2. 转-WebView loadData与loadDataWithBaseURL用法、区别

    近期用到WebView加载一些动态的内容的时候遇到一些问题,例如:在加载动态网页时,页面有很多样式表包含一些特殊字符,导致WebView无法识别产生加载异常,程序直接崩溃:另外一个就是加载的网页中有图 ...

  3. REST API出错响应的设计(转)

    REST API应用很多,一方面提供公共API的平台越来越多,比如微博.微信等:一方面移动应用盛行,为Web端.Android端.IOS端.PC端,搭建一个统一的后台,以REST API的形式提供服务 ...

  4. webview之如何设计一个优雅健壮的Android WebView?(上)(转)

    转接:https://iluhcm.com/2017/12/10/design-an-elegant-and-powerful-android-webview-part-one/ 前言 Android ...

  5. 如何设计一个优雅健壮的Android WebView?(上)

    转:如何设计一个优雅健壮的Android WebView?(上) 前言 Android应用层的开发有几大模块,其中WebView是最重要的模块之一.网上能够搜索到的WebView资料可谓寥寥,Gith ...

  6. Android WebView的loadData方法注意事项

    loadData()中的html data中不能包含'#', '%', '\', '?'四中特殊字符,出现这种字符就会出现解析错误,显示找不到网页还有部分html代码.需要如何处理呢?我们需要用Url ...

  7. Android混合开发之WebView使用总结

    前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...

  8. Android WebView使用

    转自:http://www.cnblogs.com/oakpip/archive/2011/04/08/2009800.html 大部分内容为网上整理其它高人的帖子,现只作整理,用于查看: 在Andr ...

  9. webview使用总结及注意事项

    1 网页 调用后台java代码 ,后台处理 一 网页上click事件 <a href="javascript:;" onclick="window.JsNative ...

随机推荐

  1. VMware Workstation不能启用虚拟设备floppy0由于灭有相应的有效设备在主机上. 你要尝试在每次打开虚拟机电源时连接此虚拟设备?

    编辑虚拟机的硬件,把软盘取消掉,floppy的提示就没有了

  2. 09.13日记(2014年9月13日00:18:26)英语,bootstrap,阮一峰,

    我们这里只推荐一本语法书:台湾的旋元佑老师写的<文法俱乐部>(简体版名为<语法俱乐部>).这本书因为出版社倒闭而绝版,淘宝可以买到影印的版本. (1)学英语:奶爸的英语教室 资 ...

  3. 【CF493E】【数学】Vasya and Polynomial

    Vasya is studying in the last class of school and soon he will take exams. He decided to study polyn ...

  4. IOS 学习笔记 2015-04-03 OC-API-文件读写

    // // WPFileHelper.m // OC-API-文件操作 // // Created by wangtouwang on 15/4/3. // Copyright (c) 2015年 w ...

  5. ASP.NET缓存 Cache

    缓存介绍 如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差,而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能,这样无论多 ...

  6. MongoDB笔记(三)启动命令mongod的参数

    上一节有关访问权限的笔记,是由启动命令mongod的参数auth引发的有关问题,这节就来看看mongod的其他参数 MongoDB启动命令mongod参数说明: 基本配置 --quiet # 安静输出 ...

  7. 一个供新手把玩的jQueryUI在线文档

    最近整理了一份jQueryUI文档,方便以后学习和运用. 把玩地址

  8. C# 制作卸载文件

    1.建一个控制台应用程序Uninstall: 2.在应用程序的mian方法中添加 static void Main(string[] args) { System.Diagnostics.Proces ...

  9. Spring Cloud Eureka Server例子程序

    Spring-Cloud-Eureka-Server 及Client 例子程序 参考源代码:https://github.com/spring-cloud-samples/eureka 可以启动成功, ...

  10. php学习,一个简单的Calendar(2) 一个简单的活动页面

    有了前面的基础,后面就是将页面展示出来. 预览图如下:1号和31号分别有活动,会一并显示出来   这里需要搞定几个问题,一个就是数据库的连接,我们用\sys\class\class.db_connec ...