13

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

{"regData": {
 "City":"Some City",
 "Country":"Some Country",
 "Email_Id":"abc@gmail.com",
 "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.}

Homam
  • 5,018
  • 4
  • 36
  • 39
  • 1
    your method is not generic. Check the proper method [here](http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class). Less error-prone and maintainable – Alphapico May 28 '13 at 09:58

2 Answers2

31

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.

mattt
  • 19,544
  • 7
  • 73
  • 84
  • 2
    Thanks, mattt! I had already fixed this issue. The problem was as you mentioned. I was getting string from server rather a json. – Homam May 24 '13 at 04:27
  • 1
    @mattt, I subclassed, `AFHTTPSessionManager` where can I find `operation.responseString` ? – Hemang Nov 19 '14 at 06:46
  • @Hemang did you find any solution related to "Error Domain=NSCocoaErrorDomain Code=3840" ? – Akhtar May 04 '15 at 09:22
  • Not exactly, but you can ask to your API developer to log the things, so you could check it in a browser. Maybe this is crucial but I guess for my case this did the trick :) @Akhtar – Hemang May 04 '15 at 17:31
  • 1
    Thanks you very much for the feedback @Hemang :) The problem is in the WEB API they are not sending the proper jsson string. I just log the response data and get the error.Here is the code: failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Response data: %@",operation.responseData); NSLog(@"Response String: %@",operation.responseString); NSLog(@"Error: %@", error); [self errorResponseHandlingWithResponseDict:error.userInfo WithTag:tag]; } – Akhtar May 05 '15 at 10:54
  • @matt i have same issue. but server team asking me to remove "\n" and \ is from NSData. ? but \n not there once i print on console – Avijit Nagare Dec 31 '15 at 07:23
2

Try this to get the actual error

NSLog(@"Error: %@", [error debugDescription]);
NSLog(@"Error: %@", [error localizedDescription]);
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • 4
    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.} – Homam May 10 '13 at 07:05