2011-03-20 6 views
1

안녕하세요, 저는 http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2에서 csv를 다운로드하려고 시도했으며 이후에 데이터를 구문 분석하려고했습니다. 아래 코드는 다음과 같습니다. 그것은 현재 토스트에 html 헤더 만 반환하고 있습니다. csv에서 실제 결과를 반환하지 않는 이유는 무엇입니까?HttpClient가 잘못된 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); 


      } 

답변

3

개행이 아닌 쉼표로 split이 필요하지 않습니까?

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

name, pricechange의 값이 성공적으로 채워집니다 : 나는

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

으로 코드 토스트를 위의 사용 및 교체를 실행하면

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

내가 얻을 . 나는 머리말 선을 전혀 보지 않는다.

Java 규칙은 변수 이름이 소문자로 시작하므로 rowData이 아닌 RowData입니다.

+0

예, 이것이 해결책이었습니다. 그것도 알아 낸 :). 또한 따옴표를 없애기 위해 replaceAll을 사용해야했습니다. – locoboy

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

URL을 두 개의 인수를 포함되어 제공됩니다

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를 추측은 정확합니다.

+0

네, 내가 잘못 가고 있는지 정확히 모르겠습니다. – locoboy

+1

동일한 PHP 코드 스 니펫이 있습니다. 네가 원한다면 나는 너와 그것을 나눌 수있다. –

+0

확실히보고 싶습니다. – locoboy

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> 
관련 문제