- 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];
来源: http://www.phpxs.com/code/1005519/