前言:之前公司有个java牛人,和他一起做一个新的项目,结果他的接口协议,和我之前用过的完全不一样,试了很多种方法,依旧请求不成功,最后通过其他途径找到了解决方法,这里就不再唠叨这坎坷的路了。
先贴上接口协议
req包包含包头head和包体body
{
“head”:{
“sessionID”:”xxx” //会话id(暂时不用)
},
“body”:{
}
}
body为请求具体参数
resp包包含包头head和包体body
{
“head”:{
“rct”:0, //返回错误代码 0表示正常,其他值为错误
“msg”:”正常” //错误说明
},
body:{
}
}
body内容为各个包具体返回信息
body里面就是与后台交互的主要内容。
下面是我封装的网络请求的类,get和post
+(void)GET:(NSString *)URL parameters:(NSDictionary *)dic success:(httpRequestSuccess)success failure:(httpRequestFailed)failure{
//创建配置信息
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//设置请求超时时间:5秒
configuration.timeoutIntervalForRequest = 10;
//创建会话
NSURLSession *session = [NSURLSession sessionWithConfiguration: configuration delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTPRootPath,URL]]];
//设置请求方式:POST
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-Type"];
[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Accept"];
//data的字典形式转化为data
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
//设置请求体
[request setHTTPBody:jsonData];
NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {
NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
success(responseObject);
}else{
NSLog(@"%@",error);
failure(error);
}
}];
[dataTask resume];
}
+(void)POST:(NSString *)URL parameters:(NSDictionary *)dic success:(httpRequestSuccess)success failure:(httpRequestFailed)failure{
//创建配置信息
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//设置请求超时时间:5秒
configuration.timeoutIntervalForRequest = 10;
//创建会话
NSURLSession *session = [NSURLSession sessionWithConfiguration: configuration delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTPRootPath,URL]]];
//设置请求方式:POST
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-Type"];
[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Accept"];
//data的字典形式转化为data
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
//设置请求体
[request setHTTPBody:jsonData];
NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {
NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
success(responseObject);
}else{
NSLog(@"%@",error);
failure(error);
}
}];
[dataTask resume];
}
使用的时候
NSDictionary *parm = @{@"head":@{@"sessionID":@""},@"body":@{@"userID":user,@"page":[NSString stringWithFormat:@"%d",IndexPage]}};
[NativeNetWork POST:@"getFirstPageList" parameters:parm success:^(id responseObject) {
NSLog(@"%@",responseObject);
NSDictionary * head = [responseObject objectForKey:@"head"];
NSString *ReturnMessage = [head valueForKey:@"msg"];
int ReturnCode = [[head valueForKey:@"rtc"] intValue];
if (ReturnCode == 0) {
NSDictionary * body = [responseObject objectForKey:@"body"];
if (IsRefresh) {
DataArray = [NSMutableArray arrayWithCapacity:0];
}
for (NSDictionary * dic in body) {
QuestionModel * model = [[QuestionModel alloc] init];
[model setValuesForKeysWithDictionary:dic];
[DataArray addObject:model];
}
[ListView reloadData];
}else{
[self alertViewWithTitle:nil withMessage:ReturnMessage];
}
} failure:^(NSError *error) {
[self alertViewWithTitle:@"获取列表失败" withMessage:@"请检查网络"];
}];
});
end