AFNetworking 3.0发送请求正文内容

时间:2017-07-25 02:31:39

标签: ios objective-c afnetworking afnetworking-3

我正在开发一个使用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中添加机身吗?感谢

1 个答案:

答案 0 :(得分:0)

如果我正确地提出你的问题那就不应该那么难。它也类似于2.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

        }

    }];