@interfaceViewController()
/**用来保存数据的data */
@property(nonatomic,strong)NSMutableData*receiveData;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfgetRequest];
// [self blockRequest];
// [self delegateRequest];
// [self postRequest];
}
// POST请求
- (void)postRequest
{
//将接口转换为url
NSURL*url = [NSURLURLWithString:kPostString];
//请求
NSMutableURLRequest*request = [[NSMutableURLRequestalloc]initWithURL:urlcachePolicy:0timeoutInterval:10];
//设置请求方式
[requestsetHTTPMethod:@"POST"];
NSString*string =@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData*data = [stringdataUsingEncoding:NSUTF8StringEncoding];
//拼接地址
[requestsetHTTPBody:data];
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse*_Nullableresponse,NSData*_Nullabledata,NSError*_NullableconnectionError) {
NSLog(@"%@", [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding]);
}];
}
//代理请求
- (void)delegateRequest
{
//将接口转换为url
NSURL*url = [NSURLURLWithString:kUrlString];
//请求
NSURLRequest*request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:0timeoutInterval:10];
//设置代理
// 1、向服务器发送请求,等待服务器响应
// 2、服务器返回给我们数据
// 3、接收数据完毕,开始解析
[NSURLConnectionconnectionWithRequest:requestdelegate:self];
}
//向服务器发送请求,当服务器接收到响应的时候执行此方法
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
// NSLog(@"发送请求,等待服务器响应");
//初始化容器
self.receiveData= [NSMutableDatadata];
}
//接收数据
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
// NSLog(@"接收数据");
[self.receiveDataappendData:data];
}
//数据接收完成,此方法用来解析数据
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
// NSLog(@"接收完成");
NSLog(@"%@", [[NSStringalloc]initWithData:self.receiveDataencoding:NSUTF8StringEncoding]);
}
// block请求
- (void)blockRequest
{
//将接口转换为url
NSURL*url = [NSURLURLWithString:kUrlString];
//请求
NSURLRequest*request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];
//异步请求
// NSOperationQueue mainQueue主队列
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse*_Nullableresponse,NSData*_Nullabledata,NSError*_NullableconnectionError) {
NSLog(@"%@", [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding]);
}];
}
// block请求
- (void)blockRequest
{
NSURL*url = [NSURLURLWithString:kUrlString];
NSURLRequest*request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:0timeoutInterval:10];
//单例创建
NSURLSession*session = [NSURLSessionsharedSession];
// NSURLSessionUploadTask、NSURLSessionDataTask、NSURLSessionDownLoadTask这三个方法为NSURLSessionTask的三个子类,分别代表着上传、解析、下载
NSURLSessionDataTask*task = [sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {
// NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//解析
NSDictionary*dict = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:nil];
NSArray*array = dict[@"result"][@"lists"];
for(NSDictionary*dict1inarray) {
Model*model = [[Modelalloc]init];
[modelsetValuesForKeysWithDictionary:dict1];
[self.dataArrayaddObject:model];
}
// dispatch_get_main_queue获取主队列
//回到主队列,刷新表视图
dispatch_async(dispatch_get_main_queue(), ^{
//刷新表视图
[self.tableViewreloadData];
});
}];
//必须执行此方法,要不然不会请求
[taskresume];
}
//最简单的请求
- (void)getRequest
{
//将接口转换为URL
NSURL*url = [NSURLURLWithString:kUrlString];
//请求
NSURLRequest*request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];
//发送请求给服务器
NSData*data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
//判断data是否为空
if(data ==nil) {
NSLog(@"网络不给力啊");
}
//解析
NSDictionary*dict = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:nil];
NSLog(@"%@", dict);
// NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end