1, POST 傳參 NSArray
segmentfault.com
最好的方式樊破,應(yīng)該是接口優(yōu)化茅信,不應(yīng)該多層結(jié)構(gòu)屹徘,最好是單層結(jié)構(gòu)。也就是直接傳字典沐旨,字典里全部都是簡單的基本類型數(shù)據(jù)森逮。
如果不能優(yōu)化,那么就將數(shù)組轉(zhuǎn)化成JSON字符串磁携,或者將數(shù)組中的數(shù)組按照一定的規(guī)則來來拼接褒侧,比如a|b|c這樣代替?zhèn)鲾?shù)組,后臺(tái)接收到時(shí)谊迄,就按照規(guī)則來分割得到數(shù)組了闷供。
+(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請(qǐng)求成功");
completion(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"POST請(qǐng)求失敗");
}];
}
2, 不好的 數(shù)組拼接
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated ];
[self loadLifePhotosData ];
}
用這個(gè) [self.photoUrls removeAllObjects ];
用這個(gè) self.photoUrls = nil , 就 GG 了
- (void)loadLifePhotosData
{
[self.photoUrls removeAllObjects ]; // 否則 ,有一個(gè) 拼接
NSString * urlStrin = [NSString stringWithFormat: @"%@userId=%@", kLifephoto, [MainAccountModel shareMainAccountModel].idLoginAccout ];
//下載數(shù)據(jù)
[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 ];
}