1

Hi iam new to IOS development and I am trying to POST a JSON object to a URL using AFHTTPSessionManager.

But, i am continuously getting the error The data couldn’t be read because it isn’t in the correct format.

Can someone help me to fix this. tnx. .........................................................................

Method

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

}
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
  • 1
    If you use POSTMAN (or another similar solution) and put the same parameters, does it works? You can use `[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding]` to have you currents params in JSON and use it in POSTMAN. Does it work then? – Larme Dec 09 '16 at 10:56
  • You try to add Headers . may be solve your problem. – Yogendra Girase Dec 09 '16 at 10:57
  • @Larme tnx. I check with post man it works fine. After adding your code ` *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'` i get this error. – Sathya Baman Dec 09 '16 at 11:16
  • Could you show us the log of dict? Check if all its values and "sub values" are NSString, NSNumber, NSDictionary or NSArray ? You wrote `NSString *jsonStrParam = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding]` just after `NSLog(@"signup dicxtionary : %@", dict);`, and you get the error? – Larme Dec 09 '16 at 12:24
  • I have checked in postman, I have a problem in my web service. – Arpit B Parekh Oct 16 '17 at 10:33

2 Answers2

0

Try this

You need to change as below :

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];


AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setStringEncoding:NSUTF8StringEncoding];

manager.requestSerializer=serializer;
manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/html",@"application/json", nil];
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Delete line NSLog(@"%@", [error localizedDescription]);. The String which return by [error localizedDescription] is unreadable. See Apple NSError document. enter image description here

If you want to see the error info, use NSLog(@"%@",error.userInfo[@"NSLocalizedDescription"]);

Chris.W
  • 15
  • 3