我正在从网站上读取XML Feed,但出于性能原因,我希望能够将其读入Android上的本地文件,因此我可以多次处理它而不会导致网络延迟成本。
我正在使用以下代码阅读网络文件:
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文本发送到本地文件而没有权限错误吗?
提前致谢............菲尔
greenapps,
这是logcat,但它比我已经提供的更多(用外部URL替换192.168.1.213和83.104.132.69):
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