1.请求json数据,使用AFHTTPRequestOperation完成
NSString *str=[NSString stringWithFormat:@"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.4.255.255"]; NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; // UTF-8 NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; // 请求数据,设置成功与失败的回调函数 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *html = operation.responseString; NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding]; id dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"获取到的数据为:%@",dict); }failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"发生错误!%@",error); }]; // 加入队列 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:operation];
时间: 2024-09-30 15:33:21