如何从Objective-C中的服务器下载CSV文件 [英] How to download CSV file from server in Objective-C

查看:199
本文介绍了如何从Objective-C中的服务器下载CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新的iPhone应用程序,这里我解析了一个'csv'文件从本地,并与我的工作。当我尝试从服务器以编程方式下载'csv'文件,它没有为我锻炼。



pre> - (void)viewDidLoad
{
[super viewDidLoad];

NSString * file = [[NSBundle bundleForClass:[self class]] pathForResource:@sampleofType:@csv];

NSStringEncoding encoding = 0;
NSString * csv = [NSString stringWithContentsOfFile:file usedEncoding:& encoding error:nil];
NSArray * fields = [csv CSVComponents];
NSLog(@fields:%@,fields); //获取结果内容
}

从服务器下载文件(失败)

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
NSLog (@connectionDidFinishLoading); //这里没有显示

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:%@数据); //这里没有显示

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


解决方案

p>

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

您会发现您收到的错误

  Error Domain = NSURLErrorDomain Code = -1000bad URLUserInfo = 0xf663f40 {NSUnderlyingError = 0xf663de0bad URL,NSLocalizedDescription = bad URL} 

我以前一直在调查下载信息,我想你遇到的一个问题是,单独的符号必须用+分隔。此外,当拉取索引时,您不能将^符号作为URL的一部分。您必须用%5E替换。



因此,添加:

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


I'm developing a new iPhone application, Here i have parsed a 'csv' file from local, and its working with me. When i try to download the 'csv' file from the server programmatically, it didn't workout for me. Could you please help me?

Loading data from local file (Working fine)

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

Download the file from Server (failed)

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

解决方案

Implement this method:

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

You'll find that you're getting an error of

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

I've looked into downloading information this way before, and I think one problem you're having is that separate symbols have to be separated with a "+". Also, when pulling an index, you can't pass the "^" symbol as part of the URL. You have to replace it with "%5E".

So, add this:

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

And change your URL to this:

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]];

Now it works for me! I even checked the output .csv file, and it looks good to go! One full quote per line.

这篇关于如何从Objective-C中的服务器下载CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆