1
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

这个问题是 url "dantri.com.vn/xa-hoi.rss" 更改为 "m.dantri.com.vn/xa-hoi.rss" 请帮助我!谢谢大家。

4

3 回答 3

1

确保您在清单中编写了以下内容。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-03-05T05:49:30.497 回答
1

使用Environment.getExternalStorageDirectory() 而不是静态字符串来获取 SDCARD 路径:

String sdcardpath = Environment.getExternalStorageDirectory().getAbsolutePath();
OutputStream fos = new FileOutputStream(sdcardpath+"/xa-hoi.rss");

并确保您已在以下位置添加了 sdcard 权限AndroidManifest.xml:-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-03-05T05:55:49.327 回答
0

错误在这里:

 OutputStream fos = new FileOutputStream("/sdcard/xa-hoi.rss");

用。。。来代替:

OutputStream fos = new FileOutputStream("xa-hoi.rss");
于 2013-03-05T05:48:23.737 回答