[英]How can I read a text file (not HTML) from URL in Android?
我从服务器请求一个文本文件。 网址不会以some_file.txt结尾,因为该文件是根据请求动态创建的(不确定是否确实相关)。 当我使用下面的代码时,我不会读入文本(尽管我确实以html读取,如果指向的是具有html的网址)。
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:”,但不返回任何文本。 我想念什么?
编辑:这是文件的示例。 如果将网址粘贴到地址栏中,则会在浏览器中显示:
20120120_1734
20120120_1725
20120120_1715
20120120_1705
20120120_1655
您正在吞噬解决此问题所需的所有潜在错误。
在catch子句中,尝试添加一些代码以查看错误。 类似于以下内容:
System.out.println("Error: ", e.getMessage());
您是否在清单中添加了Internet权限? 代码看起来不错,但是由于在catch子句中没有打印消息,因此您显然会遇到一些错误,并且不会输出错误。
Log.i("--->","e.getMessage()); //just like oldingn said
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.