问题:要检查网页中的一段文本:

开始我是这样写的:

  

private final static String SPECIFIED_TEXT = "这个是一段中文";

onWebView().check(webContent(containingTextInNode(SPECIFIED_TEXT )));
然后直接报错了

从adb logcat看到的结果是网页中文显示为乱码,尝试输出了一下每个中文的长度都是3;但是可以看到网页结构和数据

可以看到数据文本数据是在<p></p> <h2></h2> 里面
不死心啊: 把检查代码全部从库里面拷贝出来 改成
onWebView().check(userWebContent(containingTextInNode(SPECIFIED_TEXT , "p")));
/**
* 为了把网页输出出来
* @param xml
*/
public static void logall(String xml) {
if (xml.length() > 4000) {
for (int i = 0; i < xml.length(); i += 4000) {
if (i + 4000 < xml.length())
Log.i(TAG, xml.substring(i, i + 4000));
else
Log.i(TAG, xml.substring(i, xml.length()));
}
} else
Log.i(TAG, xml);
}
/**
* A WebAssertion which asserts that the document is matched by th provided matcher.
*/
public static WebAssertion<Document> userWebContent(final Matcher<Document> domMatcher) {
checkNotNull(domMatcher);
return webMatches(transform(script("return document.documentElement.outerHTML;"),
new TransformingAtom.Transformer<Evaluation, Document>() {
@Override
public Document apply(Evaluation eval) {
if (eval.getValue() instanceof String) {
try {
// Logall( "eval.getValue() " + (String)eval.getValue()); //这个地方能完整输出网页数据-不乱码的
// return TagSoupDocumentParser.newInstance().parse((String) eval.getValue()); //这个方法不能显示中文
org.jsoup.helper.W3CDom w3cDom = new W3CDom();
org.jsoup.nodes.Document doc = Jsoup.parseBodyFragment((String) eval.getValue()); //org.jsoup.nodes.Document无法转换为org.w3c.dom.Document
return w3cDom.fromJsoup(doc);
} catch (Exception se) {
throw new RuntimeException("Parse failed: " + eval.getValue(), se);
}
}
throw new RuntimeException("Value should have been a string: " + eval);
}
}), domMatcher,
new WebViewAssertions.ResultDescriber<Document>() {
@Override
public String apply(Document document) {
try {
DOMSource docSource = new DOMSource(document);
Transformer tf = TransformerFactory.newInstance().newTransformer();
StringWriter writer = new StringWriter();
StreamResult streamer = new StreamResult(writer);
tf.transform(docSource, streamer);
return writer.toString();
} catch (TransformerException e) {
return "Could not transform!!!" + e;
}
}
});
}
/**
* Returns a matcher that matches Documents that have a body containing the given test.
*/
public static Matcher<Document> containingTextInNode(String text, final String nodeNme) {
checkNotNull(text);
return withNodeName(withTextContent(containsString(text)), nodeNme);
} /**
* Returns a matcher that matches {@link Document}s with body that matches the given matcher.
*/
public static Matcher<Document> withNodeName(final Matcher<Element> bodyMatcher, final String nodeNme) {
checkNotNull(bodyMatcher);
return new TypeSafeMatcher<Document>() {
@Override
public void describeTo(Description description) {
description.appendText("with NodeName: ");
bodyMatcher.describeTo(description);
} @Override
public boolean matchesSafely(Document document) {
NodeList nodeList = document.getElementsByTagName(nodeNme);
if (nodeList.getLength() == 0) {
return false;
}
// showNode(nodeList, "");
for (int i = 0; i < nodeList.getLength(); i++) {
if (bodyMatcher.matches(nodeList.item(i))) {
return true;
}
}
return false;
}
};
} /**
* 将节点集放入已排序的集合中时,W3C 将其称为 NodeList;可以按从零开始的索引检索数据。
*
* @param nodeList
* @param path
*/
public static void showNode(NodeList nodeList, String path) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node mobilePhone = nodeList.item(i);
int destination = mobilePhone.getTextContent().length();
NodeList mobileNodeList = mobilePhone.getChildNodes();
if (mobileNodeList.getLength() > 0) {
showNode(mobileNodeList, path + "-" + mobilePhone.getNodeName());
} else {
Log.i(TAG, path + "-" + mobilePhone.getNodeName() + ":" + destination + " " + mobilePhone.getTextContent()); //无子节点了就显示
}
}
}
 
//上面我们用了jsoup库,gradle里面增加库依赖 
//还要注意
Document转换
dependencies {
compile 'org.jsoup:jsoup:1.9.2'
   androidTestCompile 'org.jsoup:jsoup:1.9.2' //测试用这个
}
至此可以顺利检查到网页中的中文啦,代码比较乱,将就着先用吧

onWebView检查网页中文的更多相关文章

  1. 解决Ubuntu下Chrome浏览器网页中文字体混乱

    在Ubuntu下使用Chrome浏览器时碰到了网页中文字体混乱的现象: 黑体和楷体混杂,看起来非常不美观. 这是由于许多网页并没有指定字体,然后浏览器将调用系统默认字体配置. 首先,安装文泉驿字体: ...

  2. 【转载】 IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法

    [参考了别人的文章]我们做技术,经常在写页面的时候需要多次刷新测试,可是浏览器都有自己的 缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改的CSS就看不到效果 了,每次都去清空缓存,再刷新看效 ...

  3. IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法

    浏览器都有自己的 缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改的CSS就看不到效果 了,每次都去清空缓存,再刷新看效果,这样操作太麻烦了.在IE下我们可以直接 去修改internet选项/ ...

  4. 使用notepad++学习python爬虫,print网页中文乱码问题

    今天学习使用python爬虫的时候发现爬到的网页中文会乱码,一直网上搜索解决办法,一个一个试验过去,发现还是乱码,然后我就开始使用其它方法测试,用python自带的编辑器打开是正常的,发现是notep ...

  5. 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍

    爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...

  6. [Python] - 使用chardet检查网页编码格式时发现的问题

    最近在使用chardet检查网页编码格式时发现如下问题: 用urllib打开网页再检查编码格式和用urllib2打开网页检查编码格式结果不一样,所以urllib2打开可能导致问题,需要关注. 查看了相 ...

  7. node爬虫之gbk网页中文乱码解决方案

    之前在用 node 做爬虫时碰到的中文乱码问题一直没有解决,今天整理下备忘.(PS:网上一些解决方案都已经不行了) 中文乱码具体是指用 node 请求 gbk 编码的网页,无法正确获取网页中的中文(需 ...

  8. mac下网页中文字体优化

    最近某人吐槽某门户网站在mac下chrome字体超丑,然后发现虽然现在mac用户越来越多,但是大家依然无视mac下的字体差异,于是研究了下mac下网页中的中文字体,和大家分享. 看了一遍国内各大门户和 ...

  9. Font-Spider 一个神奇的网页中文字体工具,就是这么任性

    文章摘要:    1>>  font-spider 字体神奇 由于活动项目推广的需要,页面需要用到一些漂亮好看的字体,example : 邯郸-韩鹏毛遂体.ttf. 方正喵呜.ttf 我看 ...

随机推荐

  1. MIFARE Classic S50技术详解

    Mifare Classic 简介 MIFARE Classic是恩智浦半导体开发的可用于非接触式智能卡,符合ISO/IEC 14443 A类标准.用于公共交通票证等应用,还可用于各类其他应用有S20 ...

  2. EXTJS4.0 grid 可编辑模式 配置

    首先配置这个参数 plugins:[//插件 Ext.create("Ext.grid.plugin.CellEditing",{ clicksToEdit:1//单元格 点一下就 ...

  3. 使用hadoop统计多个文本中每个单词数目

    程序源码 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Con ...

  4. CentOS 7添加本地回环地址

    CentOS 7添加本地回环地址 1. 临时添加ip addr add 10.10.1.1/32 dev lo:1重启失效2.永久添加cd /etc/sysconfig/network-scripts ...

  5. code forces 979C

    C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. FreeFileSync

    FreeFileSync is an Open-Source folder comparison and synchronization tool. It is optimized for highe ...

  7. html模板引擎jade的使用

    jade语法: #{xxx} //嵌入数据 p= xxx //嵌入数据 p #{xx} //嵌入数据 标签 html // 翻译为<html></html> div#test ...

  8. hdu 6114 百度之星复赛B T1

    Chess Problem Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子. 一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中 ...

  9. 转载:Posix线程编程指南(2)

    概念及作用 在单线程程序中,我们经常要用到"全局变量"以实现多个函数间共享数据.在多线程环境下,由于数据空间是共享的,因此全局变量也为所有线程所共有.但有时应用程序设计中有必要提供 ...

  10. Vim 自动补全成对的括号和引号

    修改后: 1 :inoremap (()<ESC>i 2:inoremap )<c-r>=ClosePair(')')<CR> 3:inoremap {{}< ...