【问题标题】:How to download CSV file from server in Objective-C如何在 Objective-C 中从服务器下载 CSV 文件
【发布时间】:2012-04-30 11:59:33
【问题描述】:

我正在开发一个新的 iPhone 应用程序,在这里我从本地解析了一个“csv”文件,它可以与我一起使用。当我尝试以编程方式从服务器下载“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];
}

【问题讨论】:

  • 数据没有进入:- "connection didReceiveData"
  • CSVComponents 方法是什么?

标签: iphone objective-c ios xcode csv


【解决方案1】:

实现这个方法:

-(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 文件,看起来不错!每行一个完整的报价。

【讨论】:

  • 或者,您可以使用 NSString 上的 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding 方法对 url 进行 http 编码。
  • 非常感谢!现在一切正常。
  • @BartVandendriessche 好点!可能会使代码更具可读性!将您的更改添加到我的代码中。
【解决方案2】:

如果您计划通过网络获取比单个 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];
}

【讨论】:

  • 这太过分了,因为内置的 NSURLConnection 完全做了他想要的,而且开销很小。您甚至不必导入任何标题!
  • 如果这是他唯一的网络代码,我同意你的看法。如果他的应用程序要通过网络获取更多数据,那么研究 AFNetworking 可能非常值得。
  • 很公平。我倾向于获得隧道视野,只专注于手头的问题。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
相关资源
最近更新 更多