您好iam新的iOS開發,我想使用AFHTTPSessionManager發佈一個JSON對象到一個URL。數據無法被讀取,因爲它不是正確的格式 - 目標c
但是,我不斷得到錯誤The data couldn’t be read because it isn’t in the correct format
。
有人可以幫我解決這個問題。 TNX。 ................................................. ........................
方法
- (void) registerUser: (NSString *)emailAdd compnyName:(NSString *) companyNam{
NSMutableDictionary *userAccount = [[NSMutableDictionary alloc] init];
[userAccount setObject:@"sighUpForm" forKey:@"$name"];
[userAccount setObject:@"true" forKey:@"$dirty"];
[userAccount setObject:@"false" forKey:@"$pristine"];
[userAccount setObject:@"true" forKey:@"$valid"];
[userAccount setObject:@"false" forKey:@"$invalid"];
[userAccount setObject:@"false" forKey:@"$submitted"];
[userAccount setObject:emailAdd forKey:@"$Email"];
[userAccount setObject:companyNam forKey:@"$CompanyName"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:companyNam forKey:@"HelpdeskDomainName"];
[dict setObject:companyNam forKey:@"CompanyName"];
[dict setObject:[NSString stringWithFormat:@"%@ : Admin",companyNam] forKey:@"FirstName"];
[dict setObject:@"" forKey:@"LastName"];
[dict setObject:emailAdd forKey:@"SendTo"];
[dict setObject:@"0" forKey:@"PreferedLanguageID"];
[dict setObject:@"" forKey:@"AffiliateId"];
[dict setObject:userAccount forKey:@"UserAccountModel"];
NSLog(@"signup dicxtionary : %@", dict);
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSLog(@"Login dicxtionary : %@", dict);
MBProgressHUD *hud;
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"Please wait....";
//manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[hud showAnimated:YES];
// send user login data to hosting via AFHTTP Async Request in AFNetworking
[manager POST:BASEURL parameters:dict progress:nil success:^(NSURLSessionTask *task, id responseObject) {
[hud hideAnimated:YES];
// Login response validation
if (responseObject == [NSNull null]) {
// [self showErrorMessage:@"Unable to login. Please try again!" title:@"Login Failed"];
}else {
//NSError *error = nil;
NSLog(@"response type : %@", NSStringFromClass([responseObject class]));
//NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
//[self checkResultValue:(NSDictionary *)responseObject];
}
} failure:^(NSURLSessionTask *task, NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}];
}
如果使用郵差(或其他類似的解決方案),並把相同的參數,它的工作原理?你可以使用'[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding]'在JSON中使用current參數並在POSTMAN中使用它。那它有用嗎? – Larme
您嘗試添加標題。可能會解決你的問題。 –
@Larme tnx。我檢查郵政人,它工作正常。在添加代碼***終止應用程序,由於未捕獲的異常'NSInvalidArgumentException',原因:'*** + [NSJSONSerialization dataWithJSONObject:options:error:]:無效的JSON頂級類型write''我得到這個錯誤。 –