JSOUP 教程,JSOUP请求JSON ,JSOUP返回JSON 数据

JSON 2016-09-13 13:10:25 111001

最近在使用 JSOUP  作为 爬虫  爬取数据,在用习惯了 JSOUP  后,因为那种链式结构,非常喜欢,故想用它来请求接口,构造请求头的时候非常方便。其实它必须是支持的,因为底层使用的还是 HttpConnection  做为处理的,代码如下:

Document doc = Jsoup
		.connect(Constant.DATA_URL)
		.header("Accept", "*/*")
		.header("Accept-Encoding", "gzip, deflate")
		.header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
		.header("Content-Type", "application/json;charset=UTF-8")
		.header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
		.timeout(10000).get();
Element body = doc.body();
JSONObject json = JSONObject.fromObject(body.text());

但是出现问题了,请求就报错:

org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/json;charset=UTF-8, URL=http://www.baidu.com/
	at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:600)
	at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
	at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)
	at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216)

当然一看就明白,说没有指定类型。找了如下解决方案:

 Response res = Jsoup.connect(Constant.DATA_URL)
		.header("Accept", "*/*")
		.header("Accept-Encoding", "gzip, deflate")
		.header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
		.header("Content-Type", "application/json;charset=UTF-8")
		.header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
		.timeout(10000).ignoreContentType(true).execute();//.get();
String body = res.body();
JSONObject json = JSONObject.fromObject(body);

上面其实关键点在于:ignoreContentType(true) ,这个是忽略请求类型。建议用execute() 去执行,如果用get 去执行的话,返回来是一个 HTML  页面包裹的 JSON  ,你处理起来稍微有点费劲。返回情况如下:

而且你要处理换行,你看到有换行了吧, JSON  正确的格式是一行返回,多行的话转成 JSON  对象的时候就会报错。所以还是推荐用execute() 去执行。

不过我最后还是换做用 HttpConnection  来解决。

InputStreamReader reader = null;
BufferedReader in = null;
try {
	URL url = new URL(Constant.DATA_URL);
	URLConnection connection = url.openConnection();
	connection.setConnectTimeout(1000);
	reader = new InputStreamReader(connection.getInputStream(), "UTF-8");
	in = new BufferedReader(reader);
	String line = null; // 每行内容
	StringBuffer content = new StringBuffer();
	while ((line = in.readLine()) != null) {
		content.append(line);
	}
	if (StringUtils.isNotBlank(content)) {
		String jsonStr = content.toString().replaceAll("\\n", "");
		data = JSONObject.fromObject(jsonStr);
	}
} catch (SocketTimeoutException e) {
	System.out.println("连接超时!!!");
} catch (JSONException e) {
	System.out.println("网站响应不是json格式,无法转化成JSONObject!!!");
} catch (Exception e) {
	System.out.println("连接网址不对或读取流出现异常!!!");
} finally {
	if (in != null) {
		try {
			in.close();
		} catch (IOException e) {
			System.out.println("关闭流出现异常!!!");
		}
	}
	if (reader != null) {
		try {
			reader.close();
		} catch (IOException e) {
			System.out.println("关闭流出现异常!!!");
		}
	}
}

建议API JSON 接口请求,还是用正常的 HTTP  请求吧。


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/174.html

转载时必须以链接形式注明原始出处及本声明。


如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
JQuery Ajax四种写法,Ajax请求返回JSON 操作Demo
Java API接口返回不是JSON的解决方案,SpringMVC返回JSON配置。
Httpclent 请求限制,判断返回类型和返回数据大小
JSOUP 教程JSOUP爬虫教程JSOUP超时分析与处理
JSOUP教程JSOUP的正确打开姿势。
JSOUP教程JSOUP 乱码处理,JSOUP生僻字乱码解决方案
Elasticsearch 随机返回数据 API
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ...
JSOUP 教程—— Java爬虫,简易入门,秒杀htmlparser
CDN 请求返回 connection reset by peer,被拦截请求解决方案
最新文章
PHP变量剖析 4
SQL全外连接剖析 119
SQL自然连接剖析 147
springboot启动原理 245
SQL右连接【RIGHT JOIN】详解及图解 450
SQL左链接【LEFT JOIN】详解及图解 357
SQL非等值连接剖析 262
SQL等链接剖析 291
SQL内连接详解及图解 385
python之numpy常用的100种数值相关方法及代码示例 231
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 679222
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674561
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 599008
免费天气API,天气JSON API,不限次数获取十五天的天气预报 565182
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 551699
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509186
Elasticsearch教程(四) elasticsearch head 插件安装和使用 479635
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 262798
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244092
Elasticsearch教程(一),全程直播(小白级别) 225115
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...