AFNetWorking请求header头增加数据

我是使用pods导入第三方AFNetWorking+TLNetWork网络请求库使用。

AFNetWork中增加请求头数据:

    NSString* url = PATH(@"%@/kitchen/add/restaurant/single/menu");
    NSString* dataJson = [self.selectMultArr yy_modelToJSONString];
    PGSearchListModel* orderModel = [self.selectMultArr firstObject];
    NSArray* submitArr = [NSArray arrayWithArray:[NSString arrayWithJsonString:dataJson]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:[Defaults valueForKey:@"userToken"] forHTTPHeaderField:@"Youge-canteen-Token"];
    //    [manager.requestSerializer setValue:@"60021010001_949dadd6871e43edb28ae33b0f5ae4ec" forHTTPHeaderField:@"Youge-canteen-Token"];
    [manager.requestSerializer setValue:[GSKeyChainDataManager readUUID] forHTTPHeaderField:@"Youge-canteen-Device"];
    [manager.requestSerializer setValue:kRegistPlatformType forHTTPHeaderField:@"Youge-canteen-Platform"];
    [manager.requestSerializer setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forHTTPHeaderField:@"Youge-canteen-Version"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [manager POST:url parameters: submitArr progress:^(NSProgress * _Nonnull uploadProgress) {
        
        NSLog(@"123");
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(responseObject[@"status"] && [[NSString stringWithFormat:@"%@",responseObject[@"status"]] isEqualToString:@"200"]){
            //成功
            //刷新列表
            [JKAlert alertText:@"添加成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            //失败
            NSString* msg = @"添加失败";
            if (responseObject[@"msg"]) {
                msg = responseObject[@"msg"];
            }
            [JKAlert alertText:msg];
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [JKAlert alertText:@"添加失败"];
        
    }];

TLNetWork增加header:

    NSMutableDictionary* headerDict = [NSMutableDictionary dictionary];
    [headerDict setObject:[Defaults valueForKey:@"userToken"] forKey:@"Youge-canteen-Token"];//token
    [headerDict setValue:[GSKeyChainDataManager readUUID] forKey:@"Youge-canteen-Device"];
    [headerDict setValue:kRegistPlatformType forKey:@"Youge-canteen-Platform"];
    [headerDict setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"Youge-canteen-Version"];
    [TLAsiNetworkHandler configHttpHeaders:headerDict];

 

model 类型(YYModel)转化为 json 字符串

//数组转化为 JSON
[self.selectMultArr yy_modelToJSONString];

//Dictionary 类型 model 转化为 JSON
[self.dishModel yy_modelToJSONString];

 

 

根据 json 字符串转化为对应的 NSArray / NSDictionary 类型方法:

//根据json字符串转化为NSDictionary类型
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败:%@",err);
        return nil;
    }
    return dic;
}
//根据json字符串转化为NSArray类型
+ (NSArray *)arrayWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSArray *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败:%@",err);
        return nil;
    }
    return dic;
}

 

 

 

 

 

 

 

你可能感兴趣的:(IOS类库,网络请求)