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是从简书的一篇短文,越来 ...
随机推荐
- C#分布式缓存Couchbase
C#分布式缓存Couchbase使用 一.简介 目前C#业界使用得最多的 Cache 系统主要是 Memcached和 Redis. 这两个 Cache 系统可以说是比较成熟的解决方案,也是很多系统当 ...
- SharePoint 2013 添加Ribbon菜单
原文:SharePoint 2013 添加Ribbon菜单 前言:今天,我们尝试一下添加SharePoint2013的Ribbon菜单,这个Ribbon菜单是由XML定义,JavaScript脚本来实 ...
- android控制控制的显示顺序
在android中假设首先在xml中静态加入了一个控件,剩下的控件都是通过addView动态加入.那么假设有控件覆盖的情况(比方说使用FrameLayout或者RelativeLayout),先加入得 ...
- oracle11g ASM(修复损坏的磁盘组头asm修复2)
--编KFED [oracle@rac2 lib]$cd $ORACLE_HOME/rdbms/lib [oracle@rac2 lib]$ pwd /u01/app/oracle/product/1 ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第六步---炮台&点击炮台加入英雄&英雄升级
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦,他说:随便写,第一别全然照搬代码:第二能够说 ...
- 中国人被“清朝GDP世界第一”忽悠了!
中国人被"清朝GDP世界第一"忽悠了!"鸦片战争前的清朝GDP世界第一",这一说法在中国流传非常广.追根溯源,最早提出这一观点的似乎是英国学者麦迪森,他的一项猜 ...
- Codeforces 443 B. Kolya and Tandem Repeat
纯粹练JAVA.... B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megab ...
- Find a way (BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 BFS搜索 目标地 并记录下来 之后再判断两段路程之和 代码: #include < ...
- jQuery中queue和dequeue的用法
jQuery中的queue和dequeue是一组很有用的方法,他们对于一系列需要按次序运行的函数特别有用.特别animate动画,ajax,以及timeout等需要一定时间的函数 queue和dequ ...
- auto tool: make -2014-1210-0001
/* *Author : DavidLin *Date : 2014-12-10pm *Email : linpeng1577@163.com or linpeng1577@gmail.com *wo ...