代码改变世界

接口文件的代码

2015-10-01 16:25  真实16  阅读(169)  评论(0编辑  收藏  举报

 NSString *str = @"http://apis.baidu.com/apistore/mobilephoneservice/mobilephone";

    NSString *telStr = @"tel=18798819422";

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",str,telStr]];

    

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    request.HTTPMethod = @"GET";

    

    [request addValue:@"c5a7d13d3e78d6ebebd957262dd39d80" forHTTPHeaderField:@"apikey"];

    

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        

        //        NSLog(@"%@",data);

        

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        NSLog(@"%@",dic);

        NSLog(@"%@",dic[@"retData"][@"carrier"]);

        NSLog(@"%@",dic[@"retData"][@"province"]);

        NSLog(@"%@",dic[@"retData"][@"telString"]);

    }];