如何从一个页面和一个方法一次向两个不同的api提供两个json请求

时间:2012-10-12 11:34:30

标签: xcode json google-api nsurlconnection

我正在处理我的新iPhone应用程序,因为我需要来自一个网址的数据和地址以及我需要获取图像的其他网址。这两个结果是json响应数据的形式,但我只得到数据的结果....
这是我的代码

 - (void)viewDidLoad

     {
         jsondataimg=[[NSMutableString alloc] initWithString:@""];


#####for this url i'm not getting the result##########
NSString *urlimg = [NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%@",name];
         //NSLog(@"%@",urlimg);
         NSURL *url1= [NSURL URLWithString:urlimg];
         NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL: url1];
         NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];

         jsonData = [[NSMutableString alloc] initWithString:@""];

            NSString *urlString = [NSString stringWithFormat: @"https://maps.googleapis.com/maps/api/place/details/json?reference=%@&sensor=false&key=your key",selectedname];
         NSLog(@" the name is%@",selectedname);
         NSLog(@" the reference is%@",selectedrefer);
            NSURL *url = [NSURL URLWithString:urlString];
            NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
            NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


         [connection release];
         [request release];
         [connection1 release];
         [request1 release];

     [super viewDidLoad];

    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {

        NSString *partialData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSString *partialData1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [jsondataimg appendString:partialData1];

        [jsonData appendString:partialData];

        [partialData release];
        [partialData1 release];

    }



    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    # pragma mark for fetching the image urls


        NSDictionary *imageurls=[jsondataimg JSONValue];

        NSDictionary*images=[imageurls objectForKey:@"responseData"];
        NSLog(@"%@",images);

        ##########here null value is displaying#######

    #pragma mark for fetching the datassss

            NSDictionary *filesJSON = [jsonData JSONValue];


            NSDictionary *address1 = [filesJSON valueForKey:@"result"];
            NSLog(@"Found %@",address1);

    }

1 个答案:

答案 0 :(得分:2)

使用此方法无需设置委托方法nsurl连接..您将获得数据......并且对于下一个数据也是如此。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=mumbai"]]];

NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *strResponse = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//    NSLog(@"%@",strResponse);

SBJSON *sbJason = [[SBJSON alloc] init];
NSMutableDictionary *getPlaceList = [sbJason objectWithString:strResponse];