开发者

HttpClient returning wrong csv?

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 ((lin开发者_运维问答e = 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);


            }


Don't you need to split on a comma rather than a newline?

String[] RowData = result.split(",");

When I run the code using the above, and replacing the Toast with

System.out.println("result = "+ result);

I get:

result = "MSFT",24.80,"+0.08%"

and the values of name, price and change 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 not RowData.


 http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2

the url you are providing is containing two arguments:

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]

So i guess the CSV that you are getting is correct.


<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜