HttpClient 返回错误的 csv?
您好,我一直在尝试从 http: 下载 csv //download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2 并一直在尝试随后解析数据。这是下面的代码。目前它只返回 toast 中的 html 标头。知道为什么它不返回 csv 中的实际结果吗?
Stock stock = new Stock();
try {
//need to call yahoo api and get csv -> parse csv for most recent price and price change
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 = "";
while ((line = reader.readLine()) != null){
result += line + "\n";
String[] RowData = result.split("\n");
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
String name = RowData[0];
String price = RowData[1];
String change = RowData[2];
stock.setPrice(Double.parseDouble(price));
stock.setTicker(name);
stock.setChange(change);
}
Hi I've been trying to download a csv from http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2 and have been trying to subsequently parse the data. Here's the code below. It's currently returning just the html header in the toast. Any ideas why it's not returning the actual results in the csv?
Stock stock = new Stock();
try {
//need to call yahoo api and get csv -> parse csv for most recent price and price change
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 = "";
while ((line = reader.readLine()) != null){
result += line + "\n";
String[] RowData = result.split("\n");
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
String name = RowData[0];
String price = RowData[1];
String change = RowData[2];
stock.setPrice(Double.parseDouble(price));
stock.setTicker(name);
stock.setChange(change);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要在逗号而不是换行符上
分割
吗?当我使用上面的代码运行代码并将 Toast 替换为
我得到:
并且
name
、price
和change
的值已成功填充。我根本没有看到标题行。请注意,Java 约定变量名称以小写字母开头,因此
rowData
而不是RowData
。Don't you need to
split
on a comma rather than a newline?When I run the code using the above, and replacing the Toast with
I get:
and the values of
name
,price
andchange
are populated successfully. I don't see a header line at all.Please note that Java convention is that variable names start with a lowercase letter, so
rowData
notRowData
.您提供的网址包含两个参数:
所以我猜您获得的 CSV 是正确的。
the url you are providing is containing two arguments:
So i guess the CSV that you are getting is correct.