2017-01-23 112 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); 
     }];