【发布时间】:2012-01-20 17:32:32
【问题描述】:
我从服务器请求一个文本文件。该 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;
这仅返回“结果:”,但不返回任何文本。我错过了什么?
编辑:这是文件的一个示例。如果将 url 粘贴到地址栏中,则会在浏览器中显示:
20120120_1734
20120120_1725
20120120_1715
20120120_1705
20120120_1655
【问题讨论】:
-
您能否发布一个此请求将生成的文件示例?
-
@DanW 我编辑了我的原始帖子以显示文件。
-
@Hap 如果你只想显示它,为什么现在使用 WebView?