try {
URL url = new URL("http://dantri.com.vn/xa-hoi.rss");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = new BufferedInputStream(url.openStream());
OutputStream fos = new FileOutputStream("/sdcard/xa-hoi.rss");
byte[] buffer = new byte[1024];
int bufferLenght = 0;
while((bufferLenght = is.read(buffer)) != -1){
fos.write(buffer, 0, bufferLenght);
}
fos.close();
fos.flush();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
LogCat:03-05 05:11:35.620:W / System.err(3437):java.io.FileNotFoundException:http://m.dantri.com.vn/xa-hoi.rss。
此问题是网址“dantri.com.vn/xa-hoi.rss”更改为“m.dantri.com.vn/xa-hoi.rss” 请帮我!。谢谢大家。
答案 0 :(得分:1)
确保您在清单中写了以下内容。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:1)
使用Environment.getExternalStorageDirectory()而不是静态字符串来获取SDCARD路径:
String sdcardpath = Environment.getExternalStorageDirectory().getAbsolutePath();
OutputStream fos = new FileOutputStream(sdcardpath+"/xa-hoi.rss");
并确保您已在AndroidManifest.xml
中添加了SD卡权限: -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:0)
错误在于:
OutputStream fos = new FileOutputStream("/sdcard/xa-hoi.rss");
替换为:
OutputStream fos = new FileOutputStream("xa-hoi.rss");