iOS自己用nsurlssion必须加线程

2017-02-14  本文已影响13人  坤哥lqk

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(queue, ^{

AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

NSURL * url = [NSURL URLWithString:urlstring];

NSURLRequest * request = [NSURLRequest requestWithURL:url];

//添加deader

NSMutableURLRequest * mutableRequest = [request mutableCopy];

[mutableRequest addValue:[NSString stringWithFormat:@"JSESSIONID=%@",myDelegate.sessionId] forHTTPHeaderField:@"Cookie"];

NSLog(@"单独打印sessionid%@",myDelegate.sessionId);

request = [mutableRequest copy];

NSLog(@"打印header验证%@", request.allHTTPHeaderFields);

NSURLSessionDataTask * datatask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000 );

NSString *retStr =[[NSString    alloc]initWithData:data encoding:enc];

NSLog(@"待办待办%@",retStr);

NSData * jsonData = [retStr dataUsingEncoding:NSUTF8StringEncoding];

NSArray  * untakenArray;//待办list数组

NSDictionary * json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];

if (json == nil)

{

NSLog(@"%@",@"json解析错误");

}

NowPage ++;

untakenArray = [json objectForKey:@"gwListInfos"];

for (NSDictionary * dict in untakenArray) {

UNtakenModel * untakemodel = [[UNtakenModel alloc]init];

[untakemodel setValuesForKeysWithDictionary:dict];

[_UNTakelistArray addObject:untakemodel];

}

NSLog(@"数组个数%lu",(unsigned long)self.UNTakelistArray.count);

dispatch_async(dispatch_get_main_queue(), ^{

/**

*  通知主线程刷新

*/

if (untakenArray != nil) {

[self.UNTakenListView reloadData];

[self.UNTakenListView.mj_footer endRefreshing];

}

else

{

NSString *title = NSLocalizedString(@"提示", nil);

NSString *message = NSLocalizedString(@"用户名或密码错误", nil);

NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);

NSString *otherButtonTitle = NSLocalizedString(@"确定", nil);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

// Create the actions.

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");

}];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"The \"Okay/Cancel\" alert's other action occured.");

}];

// Add the actions.

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

[self presentViewController:alertController animated:YES completion:nil];

}

});

}];

[datatask resume];

});

上一篇下一篇

猜你喜欢

热点阅读