很常

iOS 网络请求接口需把数组作为参数

2021-04-28  本文已影响0人  xu1Peng

 // 等待UI

    [LZBLoadingView showAndroidLoadingView:nil];

    NSMutableArray *array = [NSMutableArray array];

    for(inti =0; i < models.count; i++) {

        MarketModel*model = models[i];

        NSLog(@"===%@===",model.Id);

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];

        [dictsetValue:model.IdforKey:@"id"];

        [dictsetValue:[NSString stringWithFormat:@"%d",i] forKey:@"sort"];

        [arrayaddObject:dict];

    }

    NSString *jsonDic = [self gs_jsonStringCompactFormatForNSArray:array];

    NSLog(@"===%@===",jsonDic);

    //1. 创建一个网络请求

    NSURL *url = [NSURL URLWithString:GetMarketOptionalListlistUpdateAPI];

    //2.创建请求对象

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    // 设置请求超时

   [requestsetTimeoutInterval:10.0];

   // 设置请求方法

   [requestsetHTTPMethod:@"POST"];

    NSString  *tokenstr = [MyToolsinputToken];

    NSMutableString *uuid = [[APPIdentificationManage sharedManager] readUDID];

    NSString*uuidStr =  [uuidstringByReplacingOccurrencesOfString:@"-" withString:@""];

    NSString*uuidStrs = [uuidStrsubstringToIndex:13];

    [requestaddValue:tokenstrforHTTPHeaderField:@"Authorization"];

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

    [requestaddValue:@"equipment" forHTTPHeaderField:@"ios"];

    [requestaddValue:uuidStrsforHTTPHeaderField:@"ime"];



    NSData*parametersData = [jsonDicdataUsingEncoding:NSUTF8StringEncoding];

    // 5、设置请求报文

    [requestsetHTTPBody:parametersData];

    //3.获得会话对象

    NSURLSession *session=[NSURLSession sharedSession];

    NSURLSessionDataTask*dataTask=[sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {

            NSLog(@"网络响应:response:%@",response);

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                //需要在主线程执行的代码

                [LZBLoadingViewdismissLoadingView];

                // 返回前一页

                [self.navigationController popViewControllerAnimated:YES];

                // 重新加载数据

                if(self.delegate&& [self.delegaterespondsToSelector:@selector(updateVC)]) {

                    [self.delegateupdateVC];

                    }

            }];

        }

    ];

    //5.执行任务

    [dataTaskresume];

上一篇下一篇

猜你喜欢

热点阅读