AFNetworking 3.0发布问题

时间:2016-07-03 08:40:42

标签: ios objective-c afnetworking afnetworking-3

我需要专业人员来启发我的代码有什么问题。我正试图从2.x迁移到3.x而且我正在接受偏头痛。

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"email": email, @"password": password};
[manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSData *jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData
                                                         options:0
                                                           error:nil];
    if ([[json objectForKey:@"success"] intValue] != 1) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"" message:self.error_login delegate:nil cancelButtonTitle:self.continueButton otherButtonTitles:nil];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [alert show];
    } else {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        AccountViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"account"];
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
        [viewControllers removeLastObject];
        [viewControllers addObject:vc];
        [[self navigationController] setViewControllers:viewControllers animated:NO];
    }
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

服务器端一直显示参数为空。

非常感谢任何指导= D

2 个答案:

答案 0 :(得分:0)

创建帖子请求的代码是正确的。在我自己的服务器上尝试它,它按预期工作。因此,请检查emailpassword中的值是否为nil,并检查您的服务器代码。

您可以使用AFJSONSerializer使完成代码更容易一些。那么你不必自己转换responseObject,因为AFNetworking负责这个:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSDictionary *parameters = @{@"email": email, @"password": password};
[manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSDictionary *json = responseObject;

    if ([[json objectForKey:@"success"] intValue] != 1) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"" message:self.error_login delegate:nil cancelButtonTitle:self.continueButton otherButtonTitles:nil];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [alert show];
    } else {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        AccountViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"account"];
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
        [viewControllers removeLastObject];
        [viewControllers addObject:vc];
        [[self navigationController] setViewControllers:viewControllers animated:NO];
    }
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

答案 1 :(得分:0)

AFNetworking 3.0发布方法代码。 注意: - 将此行放在.h文件中。

+ (void)requestPostUrl: (NSString *)serviceName parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure;

注意: - 将此代码放入.m文件中。

+ (void)requestPostUrl: (NSString *)serviceName parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure {

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:error];
    NSString *argString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return argString;
}];
//here 'kBaseURL' is Web services base URL and servicesName is Web Services API name, You have to pass from declaration side.
NSString *strService = [NSString stringWithFormat:@"%@%@",kBaseURL,serviceName];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
//[SVProgressHUD showWithStatus:@"Please wait..."];
[SVProgressHUD show];
[manager POST:strService parameters:dictParams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {


    if([responseObject isKindOfClass:[NSDictionary class]]) {

        if(success) {
            [SVProgressHUD dismiss];
            success(responseObject);
        }
    }
    else
    {
        NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if(success) {
            [SVProgressHUD dismiss];
            NSLog(@"POST Response  : %@",response);
            success(response);
        }
    }
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    if(failure) {
        [SVProgressHUD dismiss];
        failure(error);
    }
}];
}