2017-01-23 97 views
-1

我正在努力获得api时,在应用程序中的api我没有得到更新的响应当我从模拟器和设备中删除应用程序它的工作fine.guys我做错了请帮助我。我已经尝试了Afnetworking简单的Api nsurl会话。获取Api不能正常工作

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

    }]; 

这里是我使用afnetworkikng击打api的代码。
谢谢

+1

dont tag swift for objC questions –

+0

发生了什么事? – shallowThought

+0

您正在使用哪种版本的AFNetworking? –

回答

0

您可以尝试使用最新版本的AFNetworking库,或者您也可以简单地使用此代码。

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

感谢您的支持。 –

+0

我面临同样的问题,它不工作。 –

0

可以安装或通过在POD文件中添加这个(POD 'AFNetworking', '〜> 3.1.0')更新通过的CocoaPods AFNetworking。

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); 
     }];