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



我正在开发我的新iPhone应用程序,因为我需要来自一个URL的数据和地址,我需要从另一个url获取图像。这两个结果采用 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);
    }

只需使用它,无需设置委托方法 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]; 

相关内容

  • 没有找到相关文章

最新更新