2016-07-03 34 views
0

我需要這裏的優點來啓發我的代碼有什麼問題。我正在嘗試從2.x遷移到3.x,並且我患有偏頭痛。AFNetworking 3.0發佈問題

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

+0

設置參數的代碼對我來說看起來是正確的。你確定'email'和'password'中有值嗎? –

+0

你可以打印這個結果'參數' –

+0

簡單的解決方案使用像這樣... http://stackoverflow.com/questions/34561215/afnetworking-3-0-migration-how-to-post-with-headers-and -http-body/36299737#36299737 – Vvk

回答

0

用於創建POST請求的代碼是否正確。嘗試在我自己的服務器上,它按預期工作。因此請檢查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); 
}]; 
+0

我在Xcode中籤入的值不是零,但服務器顯示沒有任何內容被髮布。將採取您的意見AFJSONResponseSerializer – SilverHood

+0

你可以顯示你的服務器的代碼? –

0

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); 
    } 
}]; 
}