【发布时间】:2016-12-19 15:25:41
【问题描述】:
我正在尝试制作一个简单的 Android 应用,可以从网站 (https://www.lottostat.dk/rssfeed.php) 检索彩票号码。我尝试使用此处提供的示例代码(并在下面插入):Using Java to pull data from a webpage?
示例代码在使用原始目标网站 (Using Java to pull data from a webpage?) 时效果很好,我可以在 Android Studio 的输出中读取整个底层 html 代码。但是,当我将目标网站更改为我想从 (https://www.lottostat.dk/rssfeed.php) 获取数据的网站时,没有输出(br.readLine() 返回 null)。
这可能是什么问题?我是否可能需要不同的解决方案来阅读 .php 网站(即使底层代码似乎是纯 XML)?
这是工作的原始示例代码供参考:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class DownloadPage {
public static void main(String[] args) throws IOException {
// Make a URL to the web page
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
// Get the input stream through URL Connection
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
// Once you have the Input Stream, it's just plain old Java IO stuff.
// For this case, since you are interested in getting plain-text web page
// I'll use a reader and output the text content to System.out.
// For binary content, it's better to directly read the bytes from stream and write
// to the target file.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
// read each line and write to System.out
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
【问题讨论】: