13

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.}

1
  • 1
    your method is not generic. Check the proper method here. Less error-prone and maintainable Commented May 28, 2013 at 9:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, mattt! I had already fixed this issue. The problem was as you mentioned. I was getting string from server rather a json.
@mattt, I subclassed, AFHTTPSessionManager where can I find operation.responseString ?
@Hemang did you find any solution related to "Error Domain=NSCocoaErrorDomain Code=3840" ?
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
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]; }
|
2

Try this to get the actual error

NSLog(@"Error: %@", [error debugDescription]);
NSLog(@"Error: %@", [error localizedDescription]);

1 Comment

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.}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.