免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3127 | 回复: 0

[HBase] loadData与loadDataWithBaseURL的区别 [复制链接]

论坛徽章:
0
发表于 2011-11-10 20:13 |显示全部楼层
loadData与loadDataWithBaseURL的区别




在写WebView时,感觉LoadUrl太浪费流量,而且加载起来有点慢,就考虑用其它的方法来实现。在加载页面时,如果只加载数据,页面模板提前写好放到项目中,这样就可以来更快的加载页面,用户体验会好些。

      如果不用loadUrl,省下的就只有LoadData和loadDataWithBaseURL了,下面来说下LoadData和loadDataWithBaseURL 的用法;
  1.       loadData:

  2. public void loadData (String data, String mimeType, String encoding)
  3. 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.

  4. Parameters
  5. data A String of data in the given encoding. The date must be URI-escaped -- '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.
  6. mimeType The MIMEType of the data. i.e. text/html, image/jpeg
  7. encoding The encoding of the data. i.e. utf-8, base64
复制代码
下如API中所说的,

      data:是要加载的数据类型,但在数据里面不能出现英文字符:'#', '%', '\' , '?' 这四个字符,如果有的话可以用 %23, %25, %27, %3f,这些字符来替换,在平时测试时,你的数据时,你的数据里含有这些字符,但不会出问题,当出问题时,你可以替换下。

       %,会报找不到页面错误,页面全是乱码。乱码样式见符件。

       #,会让你的goBack失效,但canGoBAck是可以使用的。于是就会产生返回按钮生效,但不能返回的情况。

       \ 和? 我在转换时,会报错,因为它会把\当作转义符来使用,如果用两级转义,也不生效,我是对它无语了。

我们在使用loadData时,就意味着需要把所有的非法字符全部转换掉,这样就会给运行速度带来很大的影响,因为在使用时,在页面stytle中会使用很多%号。页面的数据越多,运行的速度就会越慢。

      data中,有人会遇到中文乱码问题,解决办法:参数传"utf-8",页面的编码格式也必须是utf-8,这样编码统一就不会乱了。别的编码我也没有试过。
  1. public

  2. void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
  3. 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.

  4. 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.

  5. Parameters
  6. baseUrl Url to resolve relative paths with, if null defaults to "about:blank"
  7. data A String of data in the given encoding.
  8. mimeType The MIMEType of the data. i.e. text/html. If null, defaults to "text/html"
  9. encoding The encoding of the data. i.e. utf-8, us-ascii
  10. historyUrl URL to use as the history entry. Can be null.
复制代码
在使用loadDataWithBaseURL时,需要注意的就是 baseUr:虽然API上写的是要传一个Url,但我在用时,发现传一个Url并不可以,我发现这个就是一个标志位,用来标志当前页面的Key值的,而historyUrl就是一个value值,在加载时,它会把baseUrl和historyUrl传到List列表中,当作历史记录来使用,当前进和后退时,它会通过baseUrl来寻找historyUrl的路径来加载historyUrl路径来加载历史界面,需要注意的就是history所指向的必须是一个页面,并且页面存在于SD卡中或程序中(assets),loadDataWithBaseURL,它本身并不会向历史记录中存储数据,要想实现历史记录,需要我们自己来实现,也许是我的技术有限,我有了比较笨的访求来实现:就是在加载页面时,我把数据另外的写到一个html页面中,并把它保存到SD中,当点击返回时,它会通过historyUrl指向的路径来加载页面,这样就解决了历史记录问题。



上面这两种方法,我建议使用后者,虽然loadData的历史记录不需要我们自己来实现,但在使用时,我们必须把所有的%,#,\,?转换掉,在转换时,也许会遇到别的困难,我也没有测完。这就两个加载上后者比前者快一到两倍。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP