gpt4 book ai didi

android - 在 android 中获取和解析 CSV 文件

转载 作者:IT老高 更新时间:2023-10-28 21:43:05 26 4
gpt4 key购买 nike

我正在尝试从 http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2 获取 csv 文件然后解析它,以便我可以将价格和价格更改为设置两个属性的对象。有没有办法可以用 android 库做到这一点?

编辑:这是工会的当前状态(不工作):

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
String[] RowData = result.split("\n");
String name = RowData[0];
String price = RowData[1];
String change = RowData[2];

stock.setPrice(Double.parseDouble(price));
stock.setTicker(name);
stock.setChange(change);
}

最佳答案

试试这样的:

    //--- Suppose you have input stream `is` of your csv file then:

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(",");
date = RowData[0];
value = RowData[1];
// do something with "data" and "value"
}
}
catch (IOException ex) {
// handle exception
}
finally {
try {
is.close();
}
catch (IOException e) {
// handle exception
}
}

希望这会有所帮助。

关于android - 在 android 中获取和解析 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5360628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com