我正在开发一个使用AFNetworking连接API接口的项目。我的问题是如何使用包含电子邮件,deviceId的正文内容向后端发送请求。我发现许多解决方案都与AFNetworking 2.0兼容而不是3.0。
现在我正在使用SessionManager,当我初始化请求时,如何添加内容主体上下文?
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
NSString *stringData = [[NSString alloc]initWithData:jsonData encoding:NSASCIIStringEncoding];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
[manager POST:_urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSString *link = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"JSON: %@",link);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我试图把电子邮件放在& deviceId进入[manager POST参数:Dict],但它不起作用。
有人可以告诉我如何在AFNetworking 3.0中添加机身吗?感谢
答案 0 :(得分:0)
NSDictionary *parameters = @{@"username": _username.text,
@"deviceId": deviceIdString,
};
NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];
NSURL *baseURL = [NSURL URLWithString:@"http://url.com/ws/test.php"];
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
//Response
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if(error.code==-1001){
//Handle the error
}
}];