Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Nested JSON using AFNetworking

To send registration data to server, I am using JSON is in following form:

{"regData": {
 "City":"Some City",
 "Country":"Some Country",
 "Email_Id":"[email protected]",
 "MobileNumber":"+00xxxxxxxxxx",
 "UserName":"Name Of user"
 }
}

Here is how am sending.

 NSURL * url = [[NSURL alloc] initWithString:registerUrlString];
            AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
            httpClient.parameterEncoding = AFJSONParameterEncoding;
            [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
            NSDictionary * params = @{@"regData": @{
                                              @"City": self.cityField.text,
                                              @"Country": self.countryField.text,
                                              @"Email_Id": self.emailField.text,
                                              @"MobileNumber": self.numberField.text,
                                              @"UserName": self.userName.text,
                                              }
                                      };

            NSMutableURLRequest * request = [httpClient requestWithMethod:@"POST" path:registerUrlString parameters:params];
            AFHTTPRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                NSLog(@"Success: %@", JSON);

            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
            NSLog(@"Error: %@", [error debugDescription]);
            }];

            [operation start];

But unfortunately I am getting this error:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x94b3c30 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

like image 552
Homam Avatar asked May 10 '13 06:05

Homam


2 Answers

Your request is fine. The error Error Domain=NSCocoaErrorDomain Code=3840 is being returned because your server is responding with invalid JSON object. NSLog operation.responseString to see what's being sent back.

like image 163
mattt Avatar answered Oct 13 '22 21:10

mattt


Try this to get the actual error

NSLog(@"Error: %@", [error debugDescription]);
NSLog(@"Error: %@", [error localizedDescription]);
like image 43
Lithu T.V Avatar answered Oct 13 '22 20:10

Lithu T.V