美文网首页
iOS的系统网络框架

iOS的系统网络框架

作者: Mr_Lin_ | 来源:发表于2021-01-21 11:13 被阅读0次

//

//  HomeRequest.m

//  SampleApp

//

//  Created by qdsg on 2021/1/20.

//

#import "HomeRequest.h"

#import "HomeListModel.h"

@implementation HomeRequest

- (void)requestHomeList:(void(^)(NSArray *models))finish {

    NSDictionary*paramsDic =@{

        @"deptId": @"0",

        @"diagType": @"0",

        @"content": @"",

        @"scheduleTime": @"",

        @"timeInterval": @"",

        @"current": @"1",

        @"size": @"10",

        @"code":@"0",

        @"isPrescription": @"",

        @"isRecommend": @"1",

    };

    NSMutableArray *paramsAry = [[NSMutableArray alloc] initWithCapacity:0];

    [paramsAryremoveAllObjects];

    for(NSString*keyin[paramsDicallKeys]) {

        NSString*param = [NSStringstringWithFormat:@"%@=%@", key, paramsDic[key]];

        [paramsAryaddObject:param];

    }

    NSString*paramsStr = [paramsArycomponentsJoinedByString:@"&"];

    NSData *paramsData = [paramsStr dataUsingEncoding:NSUTF8StringEncoding];

    NSString *urlStr = @"https://ylttest.qdsgvision.com:10211/api/xyp/app/findDoctor/findDoctorLists";

    NSURL*url = [NSURLURLWithString:urlStr];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    request.HTTPMethod=@"POST";

//    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

//    [request setValue:@"" forHTTPHeaderField:@"Authorization"];

//    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

//    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    request.HTTPBody= paramsData;

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        NSArray*records = jsonDic[@"data"][@"records"];

//        NSLog(@"%@", jsonDic[@"message"]);

//        NSLog(@"%@", jsonDic[@"data"]);

        NSMutableArray *modelAry = [[NSMutableArray alloc] initWithCapacity:0];

        for(NSDictionary*dicinrecords) {

            HomeListModel*model = [[HomeListModelalloc]init];

            [modelcongifutationWithDic:dic];

            [modelAryaddObject:model];

        }

        if(finish) {

            finish([modelArycopy]);

        }

    }];

    [taskresume];

}

@end

相关文章

网友评论

      本文标题:iOS的系统网络框架

      本文链接:https://www.haomeiwen.com/subject/fymrzktx.html