代码之家  ›  专栏  ›  技术社区  ›  user3839375

如何在iOS中获取两个json URL的数据

  •  0
  • user3839375  · 技术社区  · 11 年前

    我只知道获取Json的数据。但我需要如何获取两个Json url的数据。 我有两个url和两个MutableArrays。有一次我只能处理一个这样的url:-

    NSURL * url=[NSURL URLWithString:@" url"];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    connection=[NSURLConnection connectionWithRequest:req delegate:self];
    if(connection)
    {
       NSLog(@"Connected");
       webData=[[NSMutableData alloc]init];
       NSLog(@"Connected2");
    }
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component
    {
    
        return arrayfirst.count;
    
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView
                 titleForRow:(NSInteger)row
                forComponent:(NSInteger)component
    {
        return arrayfirst[row];
    }
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
          inComponent:(NSInteger)component
    {
    
        one.text = arrayfirst[row];
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    
                NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    
                NSLog(@"the array is %@",al);
    
                for(NSArray *arr in al)
                {
                    [arrayfirst addObject:[arr objectAtIndex:1]];
                }
      }
    

    请告诉我如何处理两个Json url。我有两个网址 BusinessCategory BusinessLocation 我有两个 MutableArrays arrayfirst arraysecond .我知道 业务类别 数据传递到 第一方阵 。但我不知道怎么处理。

    1 回复  |  直到 11 年前
        1
  •  0
  •   joelrb    11 年前

    您必须创建两个NSURL连接并处理委托中的响应。

    So, in your header file you can have:
    
    NSURLConnection *conn1;
    NSURLConnection *conn2;
    NSMutableArray *arrayFirst;
    NSMutableArray *arraySecond;
    
    In the implementation file:
    
    //Connection 1
    NSURL *url1=[NSURL URLWithString:@"yourjsonurl1"];
    NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
    conn1=[[NSURLConnection alloc] initWithRequest:request1 delegate:self];
    
    //Connection 2
    NSURL *url2=[NSURL URLWithString:@"yourjsonurl2"];
    NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
    conn2=[[NSURLConnection alloc] initWithRequest:request2 delegate:self];
    
    //In your delegate
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        if (connection==conn1) {
            arrayFirst = [[NSMutableData alloc]init];
        }
        else if (connection==conn2){
            arraySecond = [[NSMutableData alloc]init];
        }
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        if (connection==conn1) {
            [arrayFirst appendData:data];
        }
        else if (connection==conn2){
            [arraySecond appendData:data];
        }
    }
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        if (connection==conn1) {
           NSLog(@"SOMETHING WENT WRONG WITH URL1");
        }
        else if (connection==conn2){
            NSLog(@"SOMETHING WENT WRONG WITH URL2");
        }
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
        if (connection==conn1) {
           NSLog(@"DONE WITH URL1");
        }
        else if (connection==conn2){
            NSLog(@"DONE WITH URL2");
        }
    }
    

    一种更短(也许更好)的方法是在异步操作中运行它:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *result = [NSData dataWithContentsOfURL:"yourURL"];
        id jsonResult;
        if ([result length] != 0) { jsonResult = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];}
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"done!");
        });
    });