如何在同一个viewController中执行多个JSON连接

时间:2014-02-27 23:08:20

标签: ios json nsurlconnection

在viewController中,我使用以下代码来执行JSON连接:

在viewDidLoad方法中:

 //URL definition where php file is hosted
    NSURL *url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];
    // URL request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //URL connection to the internet
    [[NSURLConnection alloc]initWithRequest:request delegate:self];


//methods to perform the connection and population of data

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc]init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
    [data appendData:thedata];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //if data received network indicator not visible
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    //array waterfalls populated via JSON from database
    categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

    NSLog(@"NUMERO DE EMPRESAS = %lu"
          , (unsigned long)[categorias count]);
}

我需要在同一个viewController中执行相同操作,但是请求另外两个不同的PHP文件再创建两个NSArrays。

我只是在viewDidLoad方法中复制代码,重命名NSURl / NSURLRequest并更改其他两个网址的URL时发现问题,但我不知道如何实现连接这两个新网址的方法。

4 个答案:

答案 0 :(得分:4)

//PROCESSING FIRST CONNECTION
NSURL *first_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];
NSURLRequest *first_connection_request = [NSURLRequest requestWithURL:first_connection_url];
NSURLConnection *first_connection=[[NSURLConnection alloc]initWithRequest:first_connection_request delegate:self];

//PROCESSING SECOND CONNECTION
NSURL *second_connection_url = [NSURL URLWithString:@"http://url_of_second_string.php"];
NSURLRequest *second_connection_request = [NSURLRequest requestWithURL:second_connection_url];
NSURLConnection *second_connection=[[NSURLConnection alloc]initWithRequest:second_connection_request delegate:self];


//methods to perform the connection and population of data

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if(connection==first_connection){
         data_for_first_connection = [[NSMutableData alloc]init];
    }
    else if(connection==second_connection){
         data_for_second_connection = [[NSMutableData alloc]init];
    }
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
    if(connection==first_connection){
         [data_for_first_connection appendData:thedata];
    }
    else if(connection==second_connection){
         [data_for_second_connection appendData:thedata];
    }
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //if data received network indicator not visible
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    if(connection==first_connection) {
        //array waterfalls populated via JSON from database
    categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

    NSLog(@"NUMERO DE EMPRESAS = %lu"
      , (unsigned long)[categorias count]);
    }
    else if(connection==second_connection){
        // PROCESS TO BE DONE FOR SECOND CONNECTION
    }
}

答案 1 :(得分:1)

request1

[NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",[dict objectForKey:@"results"]);
    }];

request2

[NSURLConnection sendAsynchronousRequest:request2 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",[dict objectForKey:@"results"]);
}];

request3

[NSURLConnection sendAsynchronousRequest:request3 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",[dict objectForKey:@"results"]);
}];

答案 2 :(得分:0)

尝试这个......可能会帮助你...

如果你有两个NSURLConnection:

NSURLConnection *Connect1 = [NSURLConnection connectionWithRequest:firstRequest delegate:self];

NSURLConnection *Connect2 = [NSURLConnection connectionWithRequest:firstRequest delegate:self];

然后像这样检查:

//执行连接和数据填充的方法

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc]init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
    [data appendData:thedata];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if( [connection isEqual: Connect1] ){
        // do connection 1 stuff here
    }
    else if( [connection isEqual: Connect2] ){
        // do connection 2 stuff here
    }  
}

答案 3 :(得分:0)

调用多个URL需要了解网络/并发性和线程。 您可以管理数据的随机“到达”并同步它们以及错误。 (您可以获取2ns URL的内容,并且过一会儿出现Url 1中的错误...)

我强烈建议您的网络人员一次发送所有JSON(如果不是很大的话...)

您有很多优势: 1)更好的网络效率(仅一个电话) 2)仅需管理一个错误 3)没有同步/连贯性问题 4)数据全有/全无逻辑 5)更具可测试性。