我正在从网站读取 XML 提要,但出于性能原因,我希望能够将其读取到 Android 上的本地文件中,这样我可以多次处理它而不会产生网络延迟和成本。
我正在使用以下代码读取 Web 文件:
private InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream instream = conn.getInputStream();
return instream;
} catch (Exception e) {
Log.i("downloadUrl", "4 - Exception: " + e.getMessage());
return null;
}
}
但是,我从 downloadUrl() 收到“4 - Exception: null”消息,这搞砸了我的文件保存代码如下:
public Boolean copyStream(String feed, String file) {
InputStream input_data = null;
FileOutputStream output_data = null;
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
int buffer_count = 0;
try {
input_data = downloadUrl(feed);
output_data = openFileOutput(file, Context.MODE_PRIVATE);
while ((len = input_data.read(buffer)) != -1) {
output_data.write(buffer, 0, len);
buffer_count++;
}
input_data.close();
output_data.close();
} catch (Exception e) {
Log.e("copyStream", "Error=" + e.getMessage() + ", feed=" + feed
+ ", file=" + file + ", buffer_count=" + buffer_count);
return false;
}
return true;
}
如果我可以解决 InputStream 问题,copyStream() 会在没有权限错误的情况下将 XML 文本发送到本地文件吗?
提前致谢…………菲尔
绿色应用程序,
这是 logcat,但它说的只是我已经提供的内容(将 192.168.1.213 替换为 83.104.132.69 以获得外部 URL):
06-19 08:05:04.414: I/downloadUrl(1108): 4 - Exception: null
06-19 08:05:04.414: E/copyStream(1108): Error=/summary.xml: open failed: EROFS (Read-only file system), feed=http://192.168.1.213/cgi-bin/beerxml_a.cgi?beer_display=Summary&beer_country=*All*&beer_brewer=*All*&beer_type=*All*, file=summary.xml