iOS 原生网络请求写法
来源:互联网 发布:爱养成2圣灵之使数据 编辑:程序博客网 时间:2024/06/11 16:20
//网络请求
NSURLSession * mySession = [NSURLSessionsharedSession];
NSURL * fullURL = [NSURLURLWithString:[NSStringstringWithFormat:@"%@/%@",host,shoplisturl]];
NSMutableURLRequest * request = [NSMutableURLRequestrequestWithURL:fullURLcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:7.0];
request.HTTPMethod= @"POST";
[request addValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];
NSString * paramStr = [NSStringstringWithFormat:@"cate_id=%@&business=%@&p=%@&sort=%@&num=%@&recommend=%@&keyword=%@&lng_lat=%@&uid=%@&token=%@",cateID,businessID,page,sort,num,recommend,keyword,lng_lat,userID,token];
request.HTTPBody= [paramStr dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionDataTask * task = [mySessiondataTaskWithRequest:requestcompletionHandler:^(NSData* _Nullable data,NSURLResponse* _Nullable response,NSError* _Nullable error) {
//判断接口是否成功返回
if (error) {
//接口访问失败
NSLog(@"error:%@",error.localizedDescription);
NSLog(@"%s",__func__);
NSInteger status =0;
if (callback) {
callback(status, @"网络不给力");
}
}else
{
//接口访问成功
//JSON解析
NSError * parseError1 =nil;
NSDictionary * dic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:&parseError1];
if (parseError1) {
NSLog(@"JSON解析错误");
return;
}
NSLog(@"data:%@", dic);
NSArray *dataArray = [dicobjectForKey:@"info"];
NSInteger status = [[dicobjectForKey:@"status"]integerValue];
if (status == 1) {
if (callback) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(status,dataArray);
});
}
}else{
// [self showHUDInWindowJustWithText:[dic objectForKey:@"info"] disMissAfterDelay:1.0];
if (callback) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(status, nil);
});
}
}
}
}];
NSURLSession * mySession = [NSURLSessionsharedSession];
NSURL * fullURL = [NSURLURLWithString:[NSStringstringWithFormat:@"%@/%@",host,shoplisturl]];
NSMutableURLRequest * request = [NSMutableURLRequestrequestWithURL:fullURLcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:7.0];
request.HTTPMethod= @"POST";
[request addValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];
NSString * paramStr = [NSStringstringWithFormat:@"cate_id=%@&business=%@&p=%@&sort=%@&num=%@&recommend=%@&keyword=%@&lng_lat=%@&uid=%@&token=%@",cateID,businessID,page,sort,num,recommend,keyword,lng_lat,userID,token];
request.HTTPBody= [paramStr dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionDataTask * task = [mySessiondataTaskWithRequest:requestcompletionHandler:^(NSData* _Nullable data,NSURLResponse* _Nullable response,NSError* _Nullable error) {
//判断接口是否成功返回
if (error) {
//接口访问失败
NSLog(@"error:%@",error.localizedDescription);
NSLog(@"%s",__func__);
NSInteger status =0;
if (callback) {
callback(status, @"网络不给力");
}
}else
{
//接口访问成功
//JSON解析
NSError * parseError1 =nil;
NSDictionary * dic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:&parseError1];
if (parseError1) {
NSLog(@"JSON解析错误");
return;
}
NSLog(@"data:%@", dic);
NSArray *dataArray = [dicobjectForKey:@"info"];
NSInteger status = [[dicobjectForKey:@"status"]integerValue];
if (status == 1) {
if (callback) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(status,dataArray);
});
}
}else{
// [self showHUDInWindowJustWithText:[dic objectForKey:@"info"] disMissAfterDelay:1.0];
if (callback) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(status, nil);
});
}
}
}
}];
[task resume];
0 0
- iOS 原生网络请求写法
- iOS-原生网络请求
- IOS 原生网络请求
- iOS 原生网络请求
- iOS 原生网络请求
- iOS 原生网络请求封装
- iOS 【原生API NSURLConnection 网络请求】
- 网络请求原生
- iOS原生网络请求使用(NSURL,NSURLRequest/NSMutableURLRequest,NSURLConnection)
- javascript-原生和jquery请求写法
- 原生js的ajax请求兼容写法
- js原生网络请求数据
- iOS 【原生API NSURLSession 网络请求(重新弄一份 格式不对!!!!!!!!!!)】
- Swift网络请求(原生解析)与TableView
- 原生 NSURL 网络请求 GET POST
- Android 原生代码实现网络请求
- iOS原生的请求数据方法
- iOS原生Http请求,get post 异步
- php和c#一致的DES加密解密算法类
- webkit基础
- 用gdb调试程序笔记: 以段错误(Segmental fault)为例
- spring websocket简单搭建
- 2016蓝桥杯基础训练——十六进制转十进制
- iOS 原生网络请求写法
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
- PictureStudio开发笔记02-多国语言界面的实现
- UIGraphicsBeginImageContext和UIGraphicsBeginImageContextWithOptions实现iOS中的截图功能
- Maven入门--概念与实例
- JMS入门(三)--Queue的使用
- 我看BI在中国
- windows rsync更换端口
- JavaScript跨域总结与解决办法