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

开始我是这样写的:

  

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. xcrun: error: active developer path

    xcrun: error: active developer path ("/Applications/Xcode 2.app/Contents/Developer") does ...

  2. User namespace

    uid和gid其实很简单,主要是为了填充文件的uid和gid,这些都是静态的,那么用户执行程序这又是什么意思呢?那么进程的权限又是指什么呢? http://blog.51cto.com/skypegn ...

  3. easyui中datagrid空数据集不刷新的解决方式

    datagrid空间可以异步请求json数据,并将新数据覆盖原有数据,重绘数据表. 但是当回来空数据集的时候,js会产生这样一条错误: TypeError: rows is null for(var ...

  4. 【BZOJ1179】[Apio2009]Atm (tarjan+SPFA)

    显而易见的tarjan+spfa...不解释了 ; type edgetype=record toward,next:longint; end; var edge1,edge2:..maxn] of ...

  5. poj 1037 三维dp

    A decorative fence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7221   Accepted: 272 ...

  6. 【飞天奔月出品】memcached四大注意事项(key长度,空格限制,最大item)

    1.   key值最大长度? memcached的key的最大长度是250个字符. 注意250是memcached服务器端内部的限制(可以修改) 如果您使用的客户端支持"key的前缀&quo ...

  7. How to secure remote desktop connections using TLS/SSL

    How to secure remote desktop connections using TLS/SSL based authentication Requirement When you ena ...

  8. 网络嗅探器Wireshark

    http://www.wireshark.org/   About Wireshark Wireshark is the world's foremost network protocol analy ...

  9. bzoj 1111 - 四进制的天平

    Description 给定 1000的十进制数, 求 最小的 四幂拆分 方案 有多少种 Solution 先大除法 \(n\log_4(n)\)次取余转化为 四进制数. 然后从 低位 往 高位 \( ...

  10. 掌握 Linux 调试技术【转】

    转自:https://www.ibm.com/developerworks/cn/linux/sdk/l-debug/index.html 您可以用各种方法来监控运行着的用户空间程序:可以为其运行调试 ...