AFNetworking 3.0 发布问题

Posted

技术标签:

【中文标题】AFNetworking 3.0 发布问题【英文标题】:AFNetworking 3.0 Post Issues 【发布时间】:2016-07-03 08:40:41 【问题描述】:

我需要这里的专业人士告诉我我的代码有什么问题。我正在尝试从 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

【问题讨论】:

设置参数的代码对我来说看起来是正确的。你确定emailpassword 中有值吗? 你能打印这个结果吗parameters 这样的简单解决方案使用......***.com/questions/34561215/… @AndréSlotta 非常确定有价值观。在运行 POST 之前,我做了一个 NSLog 来检查 【参考方案1】:

创建发布请求的代码是正确的。在我自己的服务器上尝试过,它按预期工作。所以检查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);
];

【讨论】:

我签入 Xcode 时的值不是 nil,但服务器显示没有发布任何内容。将听取您对 AFJSONResponseSerializer 的建议 你能显示你服务器的代码吗?【参考方案2】:

AFNetworking 3.0 Post 方法代码。 注意 :- 将此行放入 .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);
    
];

【讨论】:

以上是关于AFNetworking 3.0 发布问题的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 3.0 使用详解 和 源码解析实现原理

iOS开发之AFNetworking 3.0使用总结

iOS开发之AFNetworking 3.0使用总结

iOS-AFNetworking3.0上传大量(1000张)图片到服务器

[AFN]AFNetworking错误总结

IOS AFN (第三方请求)