以前给后端传参数都是post form(NSDictionary)方式,今天后端要求我用post json方式,开始直接懵逼状态,这也没用过啊,找了好多的资料,最后在AFN里面找到了AFJSONRequestSerializer,代码如下
NSDictionary*dict =@{
@"value":@(self.tempareValues),
@"timepoint":self.currentTime,
@"deviceCode":m_deviceCode,
@"iValue":@(self.ivalueNum)
};
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:self.saveJsonArray error:nil];
request.timeoutInterval=10.f;
[requestsetValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestsetValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestsetValue:YYUserToken forHTTPHeaderField:@"Authorization"];
dispatch_time_tdelayTime =dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/* NSEC_PER_SEC));
NSURLSessionDataTask*task = [managerdataTaskWithRequest:requestcompletionHandler:^(NSURLResponse*_Nonnullresponse,id _NullableresponseObject,NSError*_Nullableerror) {
if(!error) {
}else{
NSLog(@"请求失败error=%@", error);
}
}];
[taskresume];
网友评论