您好我一直在尝试从http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2下载csv,并且一直在尝试解析数据。这是下面的代码。它目前只返回吐司中的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);
}
答案 0 :(得分:3)
您是否需要使用逗号而不是换行符split
?
String[] RowData = result.split(",");
当我使用上面的代码运行代码时,用
替换ToastSystem.out.println("result = "+ result);
我明白了:
result = "MSFT",24.80,"+0.08%"
并且name
,price
和change
的值已成功填充。我根本没有看到标题行。
请注意,Java约定是变量名称以小写字母开头,因此rowData
不是RowData
。
答案 1 :(得分:0)
http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2
您提供的网址包含两个参数:
1: s=msft
-this is the yahoo finance api code for microsoft
2: f=sl1p2
- this contains 3 sub-parameters
- s [it is the company name]
- l1 [it is the company's last quote price]
- p2 [it is the price change]
所以我猜你得到的CSV是正确的。
答案 2 :(得分:0)
<?php
function getStockSite($stockLink){
if ($fp = fopen($stockLink, 'r')) {
$content = '';
while ($line = fread($fp, 1024)) {
$content .= $line;
}
}
return $content;
}
?>
<table cellpadding="0" style="width:700px;" cellspacing="0">
<tr>
<th>Country</th>
<th>Indices</th>
<th>Date</th>
<th>Price</th>
<th>Prev. Price</th>
<th>High</th>
<th>Low</th>
<th>Change</th>
</tr>
<?php
$url="http://finance.yahoo.com/d/quotes.csv?s=^BSESN&f=d1p5phgc6";
try
{
$data = getStockSite($url);
$bse=explode(",",$data);
}
catch(exception $e)
{
}
?>
<tr>
<td>INDIA</td>
<td>SENSEX</td>
<td><?php echo $bse[0];?></td>
<td><?php echo $bse[1];?></td>
<td><?php echo $bse[2];?></td>
<td><?php echo $bse[3];?></td>
<td><?php echo $bse[4];?></td>
<td><?php echo $bse[5];?></td>
<tr>
</table>