Get Api is not working properly

207 Views Asked by At

i am working on get api when hit the api in app i am not getting updated response when i remove the app from simulator and device its working fine.guys what i am doing wrong please help me.i have already tried Afnetworking simple Api nsurl session.

    NSString *strURL = [NSString stringWithFormat:@"https://mysponsers.com/m/comments/publicpost/674"];

    NSURL *url = [NSURL URLWithString:strURL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
    [httpClient getPath:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    id    json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];

        NSLog(@"RESPONSE--->1%@",json);
   } failure:^(AFHTTPRequestOperation* operation, NSError *error) {
        //fail!
        NSLog(@"Error String is %@", error.localizedDescription);

    }];

Here is the code i am using afnetworkikng for hitting the api.
Thank You

2

There are 2 best solutions below

1
Jayesh Thanki On

You can try with latest version of AFNetworking library or you can simply use this code also.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://mysponsers.com/m/comments/publicpost/674"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];
0
Mark Angelo Hernandez On

You can install or update AFNetworking via cocoapods by adding this (pod 'AFNetworking', '~> 3.1.0') in your pod file.

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

    // Change your api content-type here:
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    manager.requestSerializer = requestSerializer;

    [manager GET:@"{ Write your url here }"
      parameters:nil
        progress:nil
         success:^(NSURLSessionDataTask *operation, id responseObject) {
             NSString *response = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
             NSLog(@"Success response: %@", response);
         }
         failure:^(NSURLSessionDataTask *operation, NSError *error) {
             NSHTTPURLResponse *response = (NSHTTPURLResponse *)operation.response;
             NSLog(@"Failure response: %@", response);
         }];