Android项目---HtmlParse
在解析网站上的内容的时候,总会出现很多html的标签,一般在遇到这种数据的时候,就可以用上Html
如:
content.setText(Html.fromHtml("<html><body>" + title.getContent()+ "</body></html>", null, null));
将title.getcontent()获取的文本信息转为html格式的内容,这样,在一定程度上解决了一些由于文本格式的不同而出现特殊字符的问题。
-------------------------------------------------------------------------------------------------------------------------
官方文档:http://developer.android.com/reference/android/text/Html.html
公共类-----Html extends Object
Summary
Class Overview
This class processes HTML strings into displayable styled text. Not all HTML tags are supported.
Nested Classes
interface Html.ImageGetter Retrieves images for HTML <img> tags.
interface Html.TagHandler Is notified when HTML tags are encountered that the parser does not know how to interpret.
Public Methods
static String escapeHtml(CharSequence text)
Returns an HTML escaped representation of the given plain text.
static Spanned fromHtml(String source)
Returns displayable styled text from the provided HTML string.
static Spanned fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Returns displayable styled text from the provided HTML string.
static String toHtml(Spanned text)
Returns an HTML representation of the provided Spanned text.
Public Methods
public static String escapeHtml (CharSequence text)
Returns an HTML escaped representation of the given plain text.
public static Spanned fromHtml (String source)
Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.
This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).
This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
public static String toHtml (Spanned text)
Returns an HTML representation of the provided Spanned text.
-------------------------------------------------------------------------------------------------------------------------
但是,如果获取的文本中有很多的html标签以及图片的话,就要用到html类中的两个方法和处理图片的内部类
这里提供一个解析html格式的图文文本,返回的spanned类型可直接作为textView或者editext的settext(spanned s)参数
public class HtmlParser {
static ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
try {
URL url = new URL(source);
drawable = Drawable.createFromStream(url.openStream(), "");
drawable.setBounds(, , drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());// 设置图片的大小
} catch (MalformedURLException e) {
// TODO Auto-generated catch block } catch (IOException e) {
// TODO Auto-generated catch block
} return drawable;
}
};// 解析图片 public static Spanned GetString(String html) {// 调用该方法,传入html格式的字符串
Spanned s = Html.fromHtml(html, imageGetter, null);
return s;//返回spanned类型
} }
但是,又如果,我们想将处理图片和文本的方法放到后台进行,那就要和AsyncTask进行配合使用了。在doInBackground方法中进行处理html文本和图片
/**
* 要实现图片的显示需要使用Html.fromHtml的一个重构方法:public static Spanned
* fromHtml (String source, Html.ImageGetterimageGetter,
* Html.TagHandler
* tagHandler)其中Html.ImageGetter是一个接口,我们要实现此接口,在它的getDrawable
* (String source)方法中返回图片的Drawable对象才可以。
*/
ImageGetter imageGetter = new ImageGetter() { @Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
URL url;
Drawable drawable = null;
try {
url = new URL(source);
drawable = Drawable.createFromStream(url.openStream(),
null);
drawable.setBounds(, , drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return drawable;
}
};
HotNewsInfo title = list.get();
test = Html.fromHtml(title.getContent(), imageGetter, null);
这样就能轻松搞定,文本中出现的特殊字符和.jpg格式的图片了。
Android项目---HtmlParse的更多相关文章
- Android——eclipse下运行android项目报错 Conversion to Dalvik format failed with error 1解决
在eclipse中导入android项目,项目正常没有任何错误,但是运行时候会报错,(clean什么的都没用了.....)如图: 百度大神大多说是jdk的问题,解决: 右键项目-Properties如 ...
- eclipse — 导入android项目后识别成java项目的问题及解决
最近在eclipse导入android项目的时候遇到了奇葩问题,再此记录 遇到的问题就是:将完好的android项目导入到eclipse的时候,原本这是一个很容易的事情,但是导入成功后发现,,,靠ec ...
- 用Kotlin创建第一个Android项目(KAD 01)
原文标题:Create your first Android project using Kotlin (KAD 01) 作者:Antonio Leiva 时间:Nov 21, 2016 原文链接:h ...
- Android之什么是Activity和常用的ADB命令以及Android项目结构的认识
总结一下之前学习Android的一些内容 一: Android常用的ADB命令(adb android调试桥) 1.adb devices 查看模拟器设备并重新连接. 2.adb ki ...
- eclipse将android项目生成apk并且给apk签名
转载:http://www.cnblogs.com/tianguook/archive/2012/09/27/2705724.html 生成apk最懒惰的方法是:只要你运行过android项目,到工作 ...
- Android开发学习——Android项目的目录结构
Android项目的目录结构: 资源文件夹: 清单配置文件: Android的四大组件在使用前全部需要在清单文件中配置 <?xml version="1.0" encodin ...
- Android项目实战(二十五):Android studio 混淆+打包+验证是否成功
前言: 单挑Android项目,最近即时通讯用到环信,集成sdk的时候 官方有一句 在 ProGuard 文件中加入以下 keep. -keep class com.hyphenate.** {*;} ...
- Android项目实战(二十四):项目包成jar文件,并且将工程中引用的jar一起打入新的jar文件中
前言: 关于.jar文件: 平时我们Android项目开发中经常会用到第三方的.jar文件. 其实.jar文件就是一个类似.zip文件的压缩包,里面包含了一些源代码,注意的是.jar不包含资源文件(r ...
- 初次尝试用Kotlin实现Android项目
Kotlin: The Swift of Android 起这个文内标题的原因很简单,就是对Kotlin抱有希望--能使Android的开发更简洁.高效及安全.知道Kotlin是从简书的一篇短文,越来 ...
随机推荐
- JBehave
JBehave 上篇我们说到如何从Github上clone出一个JBehave项目,既是为了学习JBehava,也是为了熟悉下Github.从clone下来的项目看来,基本没什么问题,稍微捋一捋就可以 ...
- Cocos2d-x 脚本语言Lua使用
Cocos2d-x 脚本语言Lua使用 前面几篇博客已经把Lua的相关基础知识介绍了.本篇博客就来介绍一下,怎样在Cocos2d-x项目中使用Lua这门脚本语言进行开发.因为笔者使用的时Mac系统.所 ...
- mybatis型材xxxx.xml缺少后果返回类型
下面是一个mybatis型材xxxx.xml失踪resultMap错误: 严重: Servlet.service() for servlet [SpringMVC] in context with p ...
- ReactJS入门二
ReactJS入门学习二 ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事 ...
- DB Error: 1 "unrecognized token: ":""
在网上查找http://blog.csdn.net/heihuifeng/article/details/6561615本文 .插入的字符串需加引號', [_db executeUpdate:[NSS ...
- Dos命令将合并两个文本文件的内容
当生产线的问题,有一个放b.txt的内容被添加到a.txt这需要采取.在考虑这个问题.我的第一感觉是敲代码.阅读b.txt内容,渐进写a.txt.想起昨天在加工处理生产线600M决的方法,我用java ...
- Codeforces Round #272 (Div. 1)D(字符串DP)
D. Dreamoon and Binary time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- Eclipse UML小工具AmaterasUML的配置和使用
AmaterasUML是个人认为最好用的Eclipse UML插件,能够通过拖拽Java源文件,轻松生成类图结构.同一时候支持活动图.时序图和用例图. 它的官方下载地址是:http://sourcef ...
- Quartz.Net任务统一调度框架
山寨版Quartz.Net任务统一调度框架 TaskScheduler 在日常工作中,大家都会经常遇到Win服务,在我工作的这些年中一直在使用Quartz.Net这个任务统一调度框架,也非常好用, ...
- centos6.5 x86_64安装oracle 11.2.0.3grid
centos6.5 x86_64安装oracle 11.2.0.3grid 1.安装前的准备 工作 1.1.配置node1 1.1.1.配置虚拟机并安装centos 安装node1----- 1 ...