在android下载csv

时间:2011-11-15 19:55:38

标签: android

我试图下载一个csv文件,然后需要从中提取信息... 我尝试了以下代码......

URL url=new URL("http://download.finance.yahoo.com/d/quotes.csv?s=INFY.BO&f=sl1d1t1c1ohgv&e=.csv");
HttpURLConnection c=(HttpURLConnection)url.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
while ((len1 = is.read(buffer)) != -1) 
{
sb.append(buffer);
}
System.out.println("\n\n\nSTRING >>>>>>>>>>>>> "+sb+"\n\n\n");

现在这总是会显示一条错误,表示它意外关闭。

logcat - link

1 个答案:

答案 0 :(得分:0)

  • 确保构建sb
  • 添加权限android.permission.INTERNET
  • 使用BufferedReader从字符流中读取。 (此处未使用sb

InputStream is = c.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
    Log.i("msg", new String(line));
}