我正在努力获取api时,在应用程序中点击api我没有得到更新响应当我从模拟器和设备中删除它的工作正常。我正在做错了请帮助我。我已经尝试过Afnetworking简单Api nsurl会话。
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的代码。
谢谢
答案 0 :(得分: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];
答案 1 :(得分:0)
您可以通过cocoapods安装或更新AFNetworking,方法是在您的pod文件中添加(pod' AFNetworking','〜> 3.1.0')。
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);
}];