前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS AFNetWorking 请求json数据

iOS AFNetWorking 请求json数据

原创
作者头像
大师级码师
修改2021-10-29 10:29:50
9150
修改2021-10-29 10:29:50
举报
文章被收录于专栏:大师级码师

请求json数据,使用AFHTTPRequestOperation完成

代码语言:javascript
复制
    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];</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档