我想实现下载一个rss feed文件 比如http://blog.csdn.net/rss.html?type=Homechannel=database我实现的代码如下 /**
* 根据URL得到输入流
* @param urlStr
* @return
*/
public InputStream getInputStreamFromURL(String urlStr) {
HttpURLConnection urlConn = null;
InputStream inputStream = null;
try {
url = new URL(urlStr);
urlConn = (HttpURLConnection)url.openConnection();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
private void downloadFile(String urlString, String filename) {
//BufferedReader buffer;
try {
InputStream is = getInputStreamFromURL(urlString);
OutputStream os = openFileOutput(filename, MODE_WORLD_READABLE);
byte[] buf = new byte[1024];
int hasRead = 0;
while ((hasRead = is.read(buf)) > 0) {
os.write(buf, 0, hasRead);
}
is.close();
os.close(); } catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我通过断点监视看到 在返回输入流总是为null,是怎么回事?
* 根据URL得到输入流
* @param urlStr
* @return
*/
public InputStream getInputStreamFromURL(String urlStr) {
HttpURLConnection urlConn = null;
InputStream inputStream = null;
try {
url = new URL(urlStr);
urlConn = (HttpURLConnection)url.openConnection();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
private void downloadFile(String urlString, String filename) {
//BufferedReader buffer;
try {
InputStream is = getInputStreamFromURL(urlString);
OutputStream os = openFileOutput(filename, MODE_WORLD_READABLE);
byte[] buf = new byte[1024];
int hasRead = 0;
while ((hasRead = is.read(buf)) > 0) {
os.write(buf, 0, hasRead);
}
is.close();
os.close(); } catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我通过断点监视看到 在返回输入流总是为null,是怎么回事?
解决方案 »
- 求:关于CDMA网络根据基站信息调用Google API获取位置信息的接口文档
- android拍照与本地上传图片功能,在线等
- scrollview不支持内嵌listview,多控件应使用哪种布局
- addAccount方法创建帐号,手机重启之后,创建的帐号消失了,请问这是怎么回事?
- 请教:开发环境下,通过Android模拟器如何退回到主界面?
- 怎么实现类似360手机助手下载管理的界面
- Android 怎么实现锁频?注意:是【频】而不是【屏】
- 在面对庞大的代码时。。。。
- android客户端页卡问题
- 两个没有直接关联的Activity之间怎么传递数据?
- N个高手没有解决的难题!
- android 启动servie
urlConn.connect();//在这加上
inputStream = urlConn.getInputStream();