【发布时间】:2017-10-16 06:29:55
【问题描述】:
我正在制作一个软件,它应该从提供的 url 中检索网页的标题,并尝试 JSoup 来实现这一点。 链接主要来自 youtube,JSoup 与它们完美配合,但有时输入会采用 pdf 的形式,如下所示:http://www.ninsheetmusic.org/download/pdf/2066 那是我得到以下异常的时候:
org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/pdf, URL=http://www.ninsheetmusic.org/download/pdf/2066
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:689)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:628)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:260)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:249)
at core.Request.parseTitle(Request.java:54)
at core.Request.<init>(Request.java:29)
at core.GrakeBot.parseRequest(GrakeBot.java:161)
at core.GrakeBot.onMessage(GrakeBot.java:59)
at org.jibble.pircbot.PircBot.handleLine(PircBot.java:990)
at org.jibble.pircbot.InputThread.run(InputThread.java:92)
现在我认为 JSoup 不处理 pdf,但是我可以在这里做些什么来避免此异常并获取网页标题?
这是我现在使用的代码:
private String parseTitle(String link)
{
Document doc = null;
String title = "Title could not be retrieved";
if (getType() == RequestType.YOUTUBE)
{
try
{
doc = Jsoup.connect(getLink()).get();
title = doc.getElementById("eow-title").text();
} catch (IOException e)
{
e.printStackTrace();
}
return title;
}
else if (getType() == RequestType.SHEET)
{
try
{
doc = Jsoup.connect(getLink()).get();
title = doc.getElementsByTag("title").text();
} catch (IOException e)
{
e.printStackTrace();
}
return title;
}
else
return title;
}
【问题讨论】: