2012-04-30 202 views
4

我正在開發一個新的iPhone應用程序,在這裏我已經解析了一個來自本地的'csv'文件,它和我一起工作。當我嘗試以編程方式從服務器下載'csv'文件時,它不適合我。你可以幫我嗎?從本地文件如何從服務器中下載CSV文件Objective-C

加載數據(精細工作)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString * file = [[NSBundle bundleForClass:[self class]] pathForResource:@"sample" ofType:@"csv"]; 

    NSStringEncoding encoding = 0; 
    NSString * csv = [NSString stringWithContentsOfFile:file usedEncoding:&encoding error:nil]; 
    NSArray *fields = [csv CSVComponents]; 
    NSLog(@"fields: %@", fields); //getting the result content 
} 

從服務器下載文件(失敗)

-(void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"connectionDidFinishLoading"); //nothing showing here 

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *fullName = [NSString stringWithFormat:@"quotes.csv"]; 

    NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@",docDir,fullName]; 
    [receivedData writeToFile:fullFilePath atomically:YES]; 
} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSLog(@"data: %@", data); //nothing showing here 

    if (receivedData) 
     [receivedData appendData:data]; 
    else 
     receivedData = [[NSMutableData alloc] initWithData:data]; 
} 

- (void)loadDatafromURL 
{  
    NSURL *url = [NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC,^IXIC,^dji,^GSPC,^BVSP,^GSPTSE,^FTSE,^GDAXI,^FCHI,^STOXX50E,^AEX,^IBEX,^SSMI,^N225,^AXJO,^HSI,^NSEI&f=sl1d1t1c1ohgv&e=.csv"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [NSURLConnection connectionWithRequest:request delegate:self]; 
} 
+0

什麼是你的錯誤? –

+0

數據沒有進入: - 「connection didReceiveData」 – shebi

+0

CSVComponents方法是什麼? –

回答

5

實現此方法:

-(void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error 

你會發現你得到的

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xf663f40 {NSUnderlyingError=0xf663de0 "bad URL", NSLocalizedDescription=bad URL} 

我以前這樣看着下載信息的錯誤,我認爲你有一個問題是,不同的符號必須用「+」分隔。另外,拉取索引時,不能將「^」符號作爲URL的一部分。您必須將其替換爲「%5E」。

因此,補充一點:

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"%@", [error description]); 
} 

,並更改您的網址是:

NSString *urlString = @"http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC+^IXIC+^dji+^GSPC+^BVSP+^GSPTSE+^FTSE+^GDAXI+^FCHI+^STOXX50E+^AEX+^IBEX+^SSMI+^N225+^AXJO+^HSI+^NSEI&f=sl1d1t1c1ohgv&e=.csv"; 
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 

現在,它爲我的作品!我甚至檢查過輸出.csv文件,它看起來不錯!每行一個完整報價。

+1

或者,您可以通過在NSString上使用stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding方法對http進行http編碼。 –

+0

非常感謝!現在它很好。 – shebi

+1

@BartVandendriessche好點!可能會使更多可讀代碼!將您的更改添加到我的代碼中。 – mbm29414

0

如果你打算通過網絡獲取比單個csv更多的數據,你可以看看AFNetworking,這是一個很好的網絡操作庫。然後

A工作液看起來有點像這樣:

- (void)getCSVAsynch { 
    NSString *unescaped = @"http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC,^IXIC,^dji,^GSPC,^BVSP,^GSPTSE,^FTSE,^GDAXI,^FCHI,^STOXX50E,^AEX,^IBEX,^SSMI,^N225,^AXJO,^HSI,^NSEI&f=sl1d1t1c1ohgv&e=.csv"; 
    NSURL *url = [NSURL URLWithString:[unescaped stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"CSV: %@", [[NSString alloc] initWithBytes:[responseObject bytes] length:[responseObject length] encoding:NSUTF8StringEncoding]); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Things go boom. %@", [error localizedDescription]); 
    }]; 
    [operation start]; 
} 
+0

這是矯枉過正的,因爲內置的NSURLConnection *完全*他想要的小開銷。你甚至不需要導入任何頭文件! – mbm29414

+0

如果這是他唯一的網絡代碼,我同意你的看法。如果他的應用程序要通過網絡獲取更多數據,那麼查看AFNetworking可能非常值得。 –

+0

夠公平的。我傾向於獲得隧道視野,並專注於眼前的問題。 :-) – mbm29414