我正在编写一个IRC机器人,如果有人输入!音乐,该机器人应该从此网页http://whatthefuckshouldilistentorightnow.com/artist.php?artist=e&x=36&y=30获取艺术家名称。这是代码的一部分:
public void onMessage(String channel, String sender, String login, String hostname, String message) {
if (messageIC.startsWith("!music ")) {
String musicy = "id=\"artist\">"
try {
Document doc = Jsoup.connect("http://whatthefuckshouldilistentorightnow.com/artist.php?artist=e&x=36&y=30").get();
}
catch (IOException e){
}
String texty = doc.body().text(); // "An example link"
if (texty.contains(musicy)) {
String artisty = texty.substring(musicy);
int artsy1 = artisty.indexOf(">") + 1;
int artsy2 = artisty.indexOf("</div>");
artisty = artisty.substring(artsy1, artsy2);
sendMessage(channel, "artisty: " + artisty); // */
}
else {
sendMessage(channel, "Something went wrong.");
}
}
}
但是,我在String texty = doc.body()。text();收到错误消息。消息是:
&#34;找不到符号
symbol:method body()&#34;
任何有关错误或如何改进代码的想法都将受到赞赏。
答案 0 :(得分:0)
这是因为你在try catch中声明你的var。你应该在try-catch中使用它或者之前声明它。试试这个例子......
Document doc = null;
try {
doc = Jsoup.connect("http://whatthefuckshouldilistentorightnow.com/artist.php?artist=e&x=36&y=30").get();
} catch (IOException e){}
// rest of code