文章参考技术博客地址1:点击打开链接
NSURL *url = [NSURL URLWithString:@“”];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[@“请求体,也就是参数” dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession]; // 直接用block
// NSURLSession *section = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; // 直接用代理
__weak __typeof(self) weakSelf = self;
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
__strong __typeof(weakSelf) strongSelf = weakSelf;
NSLog(@"response---------%@",response);
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str--------%@",str);
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:Nil];
NSLog(@"dic -------%@",dic);
if ([dic[@"code"] isEqualToString:@""]) {
dispatch_async(dispatch_get_main_queue(), ^{
// 进行视图操作,必须在主线程中
});
}
else if ([dic[@"code"] isEqualToString:@""])
{
dispatch_async(dispatch_get_main_queue(), ^{
// 进行视图操作,必须在主线程中
});
}
dispatch_async(dispatch_get_main_queue(), ^{
// 进行视图操作,必须在主线程中
});
}];
[task resume];