原生 網(wǎng)絡(luò)請(qǐng)求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios"] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120];
//創(chuàng)建會(huì)話(huà)
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {
/*
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", dataStr);
*/
//json --> data
//NSJSONSerialization *jsonData = [NSJSONSerialization dataWithJSONObject:<#(id)#> options:<#(NSJSONWritingOptions)#> error:<#(NSError *__autoreleasing *)#>]
/*
options:
1.讀取reading
NSJSONReadingMutableContainers 生成可變的對(duì)象,不設(shè)置這個(gè)option,默認(rèn)是創(chuàng)建不可變對(duì)象
NSJSONReadingMutableLeaves 生成可變的字符串MutableString(iOS7+有bug)
NSJSONReadingAllowFragments 允許json數(shù)據(jù)最外層不是字典或者數(shù)組
2.寫(xiě)入writing
NSJSONWritingPrettyPrinted 生成json數(shù)據(jù)是格式化的,有換行,可讀性高
*/
//data --> json
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", dataStr);
}
}];
[task resume];