gpt4 book ai didi

java - 如何从 Android 中的 URL 读取文本文件(不是 HTML)?

转载 作者:行者123 更新时间:2023-11-29 03:50:40 25 4
gpt4 key购买 nike

我从服务器请求一个文本文件。 url 不以 some_file.txt 结尾,因为该文件是根据请求动态创建的(不确定这是否相关)。当我使用下面的代码时,我不会阅读文本(尽管我会阅读 html,如果指向包含 html 的 url)。

            String text = "RESULTS:\n";

try {

String urlString =
"http://appdata.mysite.com/httpauth/" +
"hub/DAR_param1_RTQB?" +
"method=query&list=param2";

// Create a URL
URL url = new URL(urlString);

// Read all the text returned by the server
in = new BufferedReader(new InputStreamReader(url.openStream()));

String line;
while ((line = in.readLine()) != null) {
text = text + line;
}
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (Exception e) {
} finally {
if ( in != null ) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, "Exception trying to close BufferedReader");
}
}
}

return text;

这仅返回“RESULTS:”,但不返回任何文本。我错过了什么?

编辑:这是文件的示例。如果将 url 粘贴到地址栏中,这将显示在浏览器中:

20120120_1734
20120120_1725
20120120_1715
20120120_1705
20120120_1655

最佳答案

您正在吞下解决此问题所需的所有潜在错误。

在您的 catch 子句中,尝试添加一些代码以查看错误。类似的东西:

System.out.println("Error: ", e.getMessage());

关于java - 如何从 Android 中的 URL 读取文本文件(不是 HTML)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8945458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com