美文网首页
iOS AFN 上传 JSON 参数

iOS AFN 上传 JSON 参数

作者: WS_0909 | 来源:发表于2019-01-07 14:56 被阅读0次

1.找到 AFN 内AFURLRequestSerialization.m 文件第104行方法


- (NSString *)URLEncodedStringValue {
    if (!self.value || [self.value isEqual:[NSNull null]]) {
        return AFPercentEscapedStringFromString([self.field description]);
    } else {
        return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedStringFromString([self.field description]), AFPercentEscapedStringFromString([self.value description])];
    }
}

修改成下面的

- (NSString *)URLEncodedStringValue {
    if (!self.value || [self.value isEqual:[NSNull null]]) {
        return AFPercentEscapedStringFromString([self.field description]);
    }else if(!self.field || [self.field isEqual:[NSNull null]]){
        return [NSString stringWithFormat:@"%@",self.value];
    }else {
        return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedStringFromString([self.field description]), AFPercentEscapedStringFromString([self.value description])];
    }
}


2、参数字典转成Json 字符串

    NSString* jsonStr = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:bodyDic options:0 error:nil] encoding:NSUTF8StringEncoding];


    AFHTTPSessionManager *  manager = [AFHTTPSessionManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", @"image/png",nil];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    AFJSONResponseSerializer *reponse = [AFJSONResponseSerializer serializer];

    reponse.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", @"image/png", nil];

    reponse.removesKeysWithNullValues = YES;

    manager.responseSerializer = reponse;

[manager POST:@"url" parameters:jsonStr success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        XXLog(@"guygi %@",responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XXLog(@"errii == %@",error);

    }]


相关文章

网友评论

      本文标题:iOS AFN 上传 JSON 参数

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