java.io.IOException:Connection jsoup上意外的流结束

时间:2015-12-06 15:12:59

标签: java android jsoup ioexception

我正在解析一些歌词网站,我从标题中得到错误。 我给它的URL(例如): http://www.azlyrics.com/lyrics/linkinpark/intheend.html

class GetLyrics extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
    String url = urls[0];
    String output;
    output = "If you see this, some kind of error has occupied";
    try {
        Document document = Jsoup.connect(url).get(); //I dont know how it works, its google
        document.outputSettings(new Document.OutputSettings().prettyPrint(false));//makes html() preserve linebreaks and spacing
        document.select("br").append("\\n");
        Elements lyrics = document.select("b + br + br + div"); //Search for lyrics <div> tag, that after <b> and 2 <br> tags
        String s = lyrics.html().replaceAll("\\\\n", "\n"); //Google again
        output = Jsoup.clean(s, "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false));
        output = output.replace("\n\n", "\n");
        output = output.substring(4); //Remove first enters
    }
    catch (HttpStatusException e) {
        System.err.println("404 error: " + e);
        System.err.println("Check your input data");
        output = "An 404 error has occurred, more info:\n" + e + "\nCheck your input data";
        Log.d("LyricFinder", e.toString());
    }
    catch (Exception e) {
        System.err.println("Some error: " + e);
        output = "An uknown error has occurred\nCheck your internet connection";
        Log.d("LyricFinder", e.toString());
    }
    return output;
}

protected void onPostExecute(String lyrics) {
    lyricsOutput.setText(lyrics);
}

} 日志是:

D/LyricFinder: java.io.IOException: unexpected end of stream on Connection{www.azlyrics.com:80, proxy=DIRECT@ hostAddress=85.17.159.246 cipherSuite=none protocol=http/1.1} (recycle count=0)

在eclipse控制台项目中,这段代码完美无缺(但没有这个asynctask:/)
Idk该怎么做,因为我的问题仍未得到解答

1 个答案:

答案 0 :(得分:0)

好的,在其他论坛上我找到了解决方案:

Document document = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get();
相关问题