如何通过AFNetworking做GET请求?
我添加了目录AFNetworking
和UIKit+AFNetworking
给项目经理。 我使用最新的库3.0.4
在我的类文件.m
导入文件AFNetworking.h
之后。 我发现很多示例如何向url发送请求,但是信息是旧的。
如何使GET请求到URL并从服务器获取数据? 现在函数AFHTTPRequestOperation
被删除。
这是第一次在xcode中使用库,我是初学者。
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://google.com/"]]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"http://google.com/api/pigs/" parameters:nil]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // Print the response body in text NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [operation start];
=============================================
您也可以使用AFHTTPRequestOperation:
NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init]; networkQueue.maxConcurrentOperationCount = 5; NSURL *url = [NSURL URLWithString:@"https://example.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@", string); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error); }]; [networkQueue addOperation:operation];
对于AFNetworking 3.x:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:@"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(NSURLSessionTask *operation, NSError *error) { NSLog(@"Error: %@", error); }];
please try below answer. +(void)callAFWS:(NSDictionary *)dict withURL:(NSString *)strUrl withToken:(NSString *)strToken withBlock:(dictionary)block { AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [manager.requestSerializer setValue:strToken forHTTPHeaderField:@"Authorization"]; [manager GET:[NSString stringWithFormat:@"%@/%@",WebserviceUrl,strUrl] parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (!responseObject) { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:ServerResponceError forKey:@"error"]; block(responseObject); return ; } block(responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:ServerResponceError forKey:@"error"]; block(dict); }]; }
使用AFNetworking 3.0,您需要按照以下方式提出请求:
NSURL * urlStr = [NSURL URLWithString:strURL]; NSDictionary *dictParameters = @{@"user[height]": height,@"user[weight]": weight}; AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; [manager GET:url.absoluteString parameters:dictParameters success:^(NSURLSessionTask *task, id responseObject) { NSLog(@"PLIST: %@", responseObject); } failure:^(NSURLSessionTask *operation, NSError *error) { NSLog(@"Error: %@", error); }];
(void)postDatabyAF{ NSString *url; NSDictionary *dict; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions: NSJSONReadingAllowFragments]; [manager POST:url parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { NSLog(@" operation.responseData %@",operation.responseData); NSLog(@" operation.response %@",operation.response); NSLog(@"statusCode %ld",operation.response.statusCode); } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) { if (error) { } else{ } }];
}
- 使用AFHTTPRequestOperation AFNetworking 2authentication
- 如何获得由AFNetworking的UIImageView类别存储的caching图像?
- AFNetworking代码给我内存泄漏
- 如何使用AFNetworking库与服务器进行HTTP持久连接
- 使用AFNetworking 2.0访问DailyMile API
- 如何使用AFNetworking为请求设置HTTP正文?
- (cocoa错误3840.)“(字符0附近的值无效)AFNetworking
- 如何检查互联网工作与否在ios中
- 如何将AFHTTPClient,Afnetworking 1.0迁移到2.0