HttpClient 返回错误的 csv?

发布于 2024-10-24 00:30:21 字数 1309 浏览 2 评论 0原文

您好,我一直在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小情绪 2024-10-31 00:30:21

您不需要在逗号而不是换行符上分割吗?

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

当我使用上面的代码运行代码并将 Toast 替换为

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

我得到:

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

并且 namepricechange 的值已成功填充。我根本没有看到标题行。

请注意,Java 约定变量名称以小写字母开头,因此 rowData 而不是 RowData

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.

甜宝宝 2024-10-31 00:30:21
 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 是正确的。

 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.

场罚期间 2024-10-31 00:30:21
<?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>
<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文