我正在嘗試使用AFNetworking將Json數組發送到服務器。這裏是我的JSON數組格式在使用AFNetworking發送Json數組到服務器時出錯2
{
"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"},...
]
}
這裏是我的代碼將JSON發送到服務器
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);
}];
我收到此錯誤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.)
我不知道我是什麼在這裏做錯了。我的代碼似乎對我是正確的。請幫忙。
編輯:
這是記錄我得到
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;
} }
我已經添加到代碼,但仍錯誤仍
manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:400];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
我正在使用AFNetworking 2.您的代碼來自早期版本。 – suhit