Java URLConnection不读取所有文本。 [英] Java URLConnection doesn't read all the text.

查看:64
本文介绍了Java URLConnection不读取所有文本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



我想从facebook通知Feed中读取文本并将其保存在文件中。所以这是我试过的代码。



Hi.

I want to read text from a facebook notification feed and save it in a file. So here is the code I've tried.

URL url = new URL("https://www.facebook.com/feeds/notifications.php?id=100007845703285&viewer=100007845703285&key=AWjriLTeFA0m2TfD&format=rss20");
        
        HttpsURLConnection urlCon = (HttpsURLConnection) url.openConnection();
        
        InputStream stream = urlCon.getInputStream();
        
        byte[] streamBytes = new byte[stream.available()];
        stream.read(streamBytes);
        stream.close();

        FileOutputStream writer = new FileOutputStream("feed.txt");
        writer.write(streamBytes);





但问题是这不会从该Feed中检索所有文本。 。



我试图使用URLConnection和HttpURLConnection而不是HttpsURLConnection。但它也没有用。输出不包含至少一半的Feed。



我的代码有什么问题?是否有任何其他类读取网页的整个内容而不是URL或URLConnection?



提前致谢!



But the problem is this doesn't retrieve all the text from that feed..

I've tried to use both URLConnection, and HttpURLConnection instead of HttpsURLConnection. But it didn't work either. The output doesn't contain at least half of the feed.

What's the wrong with my code ? and is there any other class that reads the whole content of a web page instead of a URL or URLConnection ?

Thanks in advance !

推荐答案

stream.available()方法,仅返回在调用时可用的数据量,但这不一定是服务器持有的所有数据。您需要继续阅读和解析内容,直到您到达网页的末尾。请参阅 http://docs.oracle.com/javase/7 /docs/api/java/io/InputStream.html#available() [ ^ ]。
The stream.available() method, only returns how much data may be available at the point of calling, but that is not necessarily all the data that is held by the server. You need to keep reading and parsing the content until you get to the end of the web page. See http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available()[^].


这篇关于Java URLConnection不读取所有文本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆