1, POST 传参 NSArray
segmentfault.com
最好的方式,应该是接口优化,不应该多层结构,最好是单层结构。也就是直接传字典,字典里全部都是简单的基本类型数据。
如果不能优化,那么就将数组转化成JSON字符串,或者将数组中的数组按照一定的规则来来拼接,比如a|b|c这样代替传数组,后台接收到时,就按照规则来分割得到数组了。
+(void)postHttpRequestWithURLStr:(NSString )urlStr withParametersArray:(NSArray )parameters withResponseBlock:(void(^)(id sender))completion{
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.responseSerializer = [AFHTTPResponseSerializer serializer];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
session.requestSerializer = [AFJSONRequestSerializer serializer];
session.responseSerializer = [AFJSONResponseSerializer serializer];
[session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[session.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[session POST:urlStr parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject)
{
NSLog(@"POST请求成功");
completion(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"POST请求失败");
}];
}
2, 不好的 数组拼接
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated ];
[self loadLifePhotosData ];
}
用这个 [self.photoUrls removeAllObjects ];
用这个 self.photoUrls = nil , 就 GG 了
- (void)loadLifePhotosData
{
[self.photoUrls removeAllObjects ]; // 否则 ,有一个 拼接
NSString * urlStrin = [NSString stringWithFormat: @"%@userId=%@", kLifephoto, [MainAccountModel shareMainAccountModel].idLoginAccout ];
//下载数据
[ZheProgressHUD showOnView: self.view ];
__weak typeof(self) weakSelf = self;
// 问题
[GetNetWorkDataMethod getDataWithParameters: nil requestType:ZheNetworkingRequestTypeGet andWithURLStr: urlStrin userInfo: nil success:^(NSURLSessionDataTask *task, NSDictionary *responseObject, NSDictionary *userInfo) {
//responseObject;
for (NSDictionary * dictionaryTemp in responseObject){
NSString * urlStri = [dictionaryTemp valueForKey: @"Url" ];
[weakSelf.photoUrls addObject: urlStri ];
}