0

This is the answer I get from API:

enter image description here

- (void)postRequestWithHeader:(NSString *)url
     parameters:(NSDictionary *)parameters
     completionHandler:(void (^)(NSDictionary *data, NSError *))completionHandler
    {
NSString *URLString = [NSString stringWithFormat:@"%@%@", baseURL, url];


NSMutableURLRequest *request= [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];
  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSString *session =[[LocalDataBase sharedDataBase]userDataObjectForKey:@"session"];
[request setValue:session forHTTPHeaderField:@"session"];


NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request
                                            completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                                                NSDictionary *backParam = [[AFHTTPResponseSerializer serializer] responseObjectForResponse:responseObject data:responseObject error:nil];

                                                if (error) {
                                                    NSLog(@"Error: %@", error);
                                                    completionHandler(nil, error);
                                                } else {
                                                    if ([responseObject isKindOfClass:[NSArray class]]) {
                                                        NSLog(@"response is in array");
                                                                                                                     }else {
                                                    NSLog(@"%@ %@", response, responseObject);
                                                    completionHandler(backParam, nil);
                                                    }
                                                    ;}
                                            }];
[dataTask resume];



    }


-(void) requestForPosts:(void (^)(NSError *error))completionHandler
   {

[self postRequestWithHeader:@"posts/list"
             parameters:nil
      completionHandler:^(NSDictionary *data, NSError *error)  {
    if (error) {
        NSLog(@"Error: %@", error);
        completionHandler(error);
    } else {
        NSLog(data);
       // [[LocalDataBase sharedDataBase] saveUserData:data];
        completionHandler(nil);
    }

}];
}

This is code i use to send request for API and then i get response and check if it is array, but i don't know what tot do after this I need to get this response and put into table view/ I hope someone will help me)

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87

1 Answers1

0

Your webservice gives you a json response.

In your completion block, the parameter data is not of kind NSDictionary but NSArray*

[[AFHTTPResponseSerializer serializer] responseObjectForResponse:responseObject data:responseObject error:nil]

did already parse the object for you in an NSArray, not into a NSDictionary...

How do I parse JSON with Objective-C?

Community
  • 1
  • 1
Florian Burel
  • 3,408
  • 1
  • 19
  • 20