gpt4 book ai didi

ios - 使用 AFNetworking 2 将 Json 数组发送到服务器时出错

转载 作者:行者123 更新时间:2023-11-29 10:49:41 25 4
gpt4 key购买 nike

我正在尝试使用 AFNetworking 发送 Json 数组到服务器。这是我的 Json 数组格式

{
"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 文本未以数组或对象开头,并且允许片段的选项未设置。)

我不知道我在这里做错了什么。我的代码对我来说似乎是正确的。请帮忙。

编辑:

这是我得到的日志

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"];

最佳答案

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;

关于ios - 使用 AFNetworking 2 将 Json 数组发送到服务器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21014814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com