How to send array contain dictionary to server in ios?

1.3k views Asked by At

hi i want to send a array of data to server array like

{ FeedStats: [{
   FeedId: 1,
   FeedIndex: 1,
},
{
   FeedId: 2,
   FeedIndex: 21,
}]}

i am try using afnetworking

 NSMutableArray *tempArray = [[NSMutableArray alloc]init];
 NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                            currentPerson.feedId, @"FeedId",
                                            currentPerson.articleIndex, @"FeedIndex",
                                            nil];
 NSDictionary *tempDictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:
                                            currentPerson1.feedId, @"FeedId",
                                            currentPerson1.articleIndex, @"FeedIndex",
                                            nil];
[tempArray addObject:tempDictionary];
[tempArray addObject:tempDictionary1];

NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys:tempArray,@"FeedStats", nil];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://xxx.xx/"]];
    [httpClient setAuthorizationHeaderWithToken:authToken];
    [httpClient setDefaultHeader:@"Content-Type" value:@"application/json"];

    [httpClient postPath:@"xx/xxx" parameters:tempDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Request Successful, response '%@'", responseStr);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
    }];

but array is going to Empty in server got only

FeedStats:[] 

how to fix that

2

There are 2 answers

0
sabeer On BEST ANSWER

i got a sollution by adding

httpClient.parameterEncoding = AFJSONParameterEncoding
0
Grzegorz Krukowski On

Change:

NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys:tempReportArray,@"FeedStats", nil];

To:

NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys:tempArray,@"FeedStats", nil];

tempReportArray => tempArray