本文主要是介绍AFN 3.0 网络请求使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里我简单的做了一层封装, 只进行了数据的请求
.h
/*** 数据请求** @param urlstring URL* @param parmas 请求参数* @param success 请求成功的block* @param fail 请求失败的block*/
+ (void)ZJNetworkingGetRequestWithURL:(NSString *)urlparameter:(NSDictionary *)parametersuccessBlock:(void (^)(id object))successBlockfailureBlock:(void (^)(id failure))failureBlock;
.m
+ (void)ZJNetworkingGetRequestWithURL:(NSString *)urlparameter:(NSDictionary *)parametersuccessBlock:(void (^)(id object))successBlockfailureBlock:(void (^)(id failure))failureBlock
{Reachability *reachablity = [Reachability reachabilityWithHostName:@"www.baidu.com"];if ([reachablity currentReachabilityStatus] == ReachableViaWiFi || [reachablity currentReachabilityStatus] == ReachableViaWWAN){AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", nil];[manager GET:url parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {successBlock(responseObject);} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {}];}else{}
}
使用
// 这里的ZJNetworking 类就是简单的封装工具类名
[ZJNetworking ZJNetworkingGetRequestWithURL:url parameter:nil successBlock:^(id object) {NSLog(@"object = %@", object);} failureBlock:^(id failure) {NSLog(@"失败");}];
这篇关于AFN 3.0 网络请求使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!