1

我正在开发我的新 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);

    }
4

1 回答 1

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]; 
于 2012-10-12T11:37:45.573 回答