2014-01-09 48 views
1

我正在嘗試使用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"]; 

回答

0

希望它會工作....

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) { 

      }]; 
+0

我正在使用AFNetworking 2.您的代碼來自早期版本。 – suhit

0

您的代碼似乎對我來說是正確的,它看起來已經非常正確地創建了JSON對象。您是否檢查過responseObject服務器正在返回的內容。我想responseObject不是有效的JSON併產生此錯誤。

+0

嘿,你檢查服務器的JSON嗎? – san

+0

我添加了來自服務器的響應,請檢查上面的編輯 – suhit

+0

我在響應/日誌中看不到JSON類型的對象。看來你的服務器沒有正確發送JSON。 – san

0

在您的服務器端確保您已將正確的內容類型設置爲「application/json」。我有同樣的問題,因爲我的一個調用缺少內容類型頭,顯示響應是JSON。

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

頂部頂部頂部!!!! –

相關問題