1

I am trying to send the Json array using AFNetworking to the server. Here is my Json array format

{
    "friendList": [
      {"id": 1, "username": "user1", "name":"person1", "friendUsername":"fUser1", "friendName":"fName1"},
      {"id": 2, "username": "user2", "name":"person2", "friendUserID":"fUser2", "friendName":"fName2"},
      {"id": 3, "username": "user3", "name":"person3", "friendUserID":"fUser3", "friendName":"fName3"},...
    ]
}   

Here is my code to send the Json to the server

    NSMutableArray *friendsArray = [NSMutableArray new];
    NSDictionary *params1 = @{@"id": @"1",
                             @"username":@"user1",
                             @"name":@"person1",
                             @"friendUsername":@"fUser1",
                             @"friendName":@"ffName1"};

    NSDictionary *params2 = @{@"id": @"2",
                             @"username":@"user2",
                             @"name":@"person2",
                             @"friendUsername":@"fUser2",
                             @"friendName":@"ffName2"};

    NSDictionary *params3 = @{@"id": @"3",
                             @"username":@"user3",
                             @"name":@"person3",
                             @"friendUsername":@"fUser3",
                             @"friendName":@"ffName3"};

    [friendsArray addObject:params1];
    [friendsArray addObject:params2];
    [friendsArray addObject:params3];


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];


NSDictionary *parameters = @{@"friendsList":friendsArray};
NSLog(@"%@", parameters);

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:@"http://localhost:8080/RestWebServices/Rest/Post" parameters:parameters    success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    NSLog(@"Error: %@", operation.response);
}];

I am getting the 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.)

I don't know what I am doing wrong here. My code seems correct to me. Please help.

EDIT:

this is log i am getting

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x8b63c40 {NSDebugDescription=Invalid value around character 0.}
2014-01-09 21:57:25.497 ServerJsonParsingExample[1431:70b] status code = 400
2014-01-09 21:57:25.497 ServerJsonParsingExample[1431:70b] for operation: <NSHTTPURLResponse: 0x8e611e0> { URL: http://localhost:8080/RestfulWebServices/Rest/Seemo } { status code: 400, headers {
    Connection = close;
    "Content-Type" = "text/plain";
    Date = "Thu, 09 Jan 2014 16:27:25 GMT";
    Server = "Apache-Coyote/1.1";
    "Transfer-Encoding" = Identity;
} }

I have added to the code but still the error remains

manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:400];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

4 Answers 4

1
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;
Sign up to request clarification or add additional context in comments.

Comments

0

Hope It will work....

    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:[NSURL urlWithString:@"http://localhost:8080/RestWebServices/Rest/Post"];
   [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
   [httpClient setDefaultHeader:@"Accept" value:@"application/json"];    
    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    httpClient.parameterEncoding = AFJSONParameterEncoding;

    [httpClient postPath:nil parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            }];

1 Comment

I am using AFNetworking 2. Your code is from earlier version.
0

Your code seems correct to me and it looks you have created the JSON object very much right. Have you checked what the server is returning in responseObject. I guess responseObject is not the valid JSON and generating this error.

4 Comments

Hey, did you check the JSON coming from server?
I have added the response coming from the server please check the edit above
I donot see the JSON type of object in your response/log. It seems your server is not sending the JSON correctly.
I created the client in server side and tested the response if gives me correct json, I don't know why it gives "text/plain" as content-type. When I sending the GET request it works perfectly and gives me correct json response from iOS app.
0

On your server side ensure you've set the correct content type to "application/json". I had the same issue because one of my calls was missing a content type header showing the response was JSON.

Comments

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.