2012-04-30 12 views
4

私は新しいiPhoneアプリケーションを開発しています。ここで私はローカルの「csv」ファイルを解析してくれました。私はプログラムでサーバーから 'csv'ファイルをダウンロードしようとすると、私のために運動しませんでした。手伝っていただけませんか?Objective-CのサーバーからCSVファイルをダウンロードするには

ローカルファイルからのデータのロード(ワーキング罰金)

- (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]); 
} 

を、これにあなたのURLを変更します。

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行に1つの完全な引用符。

+1

NSStringのstringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncodingメソッドを使用して、URLをhttpエンコードすることもできます。 –

+0

ありがとうございます!今それはうまくいく。 – shebi

+1

@BartVandendriessche良い点!おそらくもっと読みやすいコードを作るでしょう!あなたの変更を私のコードに加えました。 – mbm29414

0

ネットワーク経由でこの単一のcsvよりも多くのデータを取得する予定がある場合は、AFNetworkingをご覧ください。これはネットワーク操作のための素晴らしいライブラリです。

作業溶液は、ビットのようになります。

- (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

関連する問題