//1.创建会话对象
NSURLSession *session = [NSURLSession sharedSession];
//2.根据会话对象创建task
NSURL *url = [NSURL URLWithString:@"http://v.juhe.cn/wzpoints/query"];
//3.创建可变的请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//4.修改请求方法为POST
request.HTTPMethod = @"POST";
//5.设置请求体
request.HTTPBody = [@"key=6d96ee265cc090b418ee51257ff9c48f&lat=31.3348484&lon=120.6067963&" dataUsingEncoding:NSUTF8StringEncoding];
//6.根据会话对象创建一个Task(发送请求)
/*
第一个参数:请求对象
第二个参数:completionHandler回调(请求完成【成功|失败】的回调)
data:响应体信息(期望的数据)
response:响应头信息,主要是对服务器端的描述
error:错误信息,如果请求失败,则error有值
*/
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//8.解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",dict);
for (NSDictionary *dic in dict[@"result"][@"list"]) {
Model *mm=[Model new];
[mm setValuesForKeysWithDictionary:dic];
[self.arrdata addObject:mm];
NSLog(@"=====%@===",self.arrdata);
}
}];
//7.执行任务
[dataTask resume];
IOS post解析
最新推荐文章于 2023-11-26 20:35:43 发布