iOS自己用nsurlssion必須加線程

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(@"單獨(dú)打印sessionid%@",myDelegate.sessionId);

request = [mutableRequest copy];

NSLog(@"打印header驗(yàn)證%@", 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數(shù)組

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(@"數(shù)組個數(shù)%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];

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容