亲测可用,能正常获取数据(已后台返回的json数据作为测试的)
//
// NetworkTool.h
//
// Created by zhouyu on 16/10/25.
// Copyright © 2016年 mac All rights reserved.
//
#import
#import
typedef enum{
requestTypeGET,
requestTypePOST
}requestType;
@interface NetworkTool : AFHTTPSessionManager
//单例
+ (instancetype)shareNetworkTool;
//第一次封装参数比较多,比较长
- (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters progress:(nullable void (^)(NSProgress *_Nonnull downloadProgress))downloadProgress success:(nullable void (^)(NSURLSessionDataTask *_Nullable task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *_Nonnull error))failure;
//第二次封装,简化参数,把没有用的progress参数去掉
- (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success failure:(nullable void (^)(NSError *_Nonnull error))failure;
//第三次封装,直接使用类方法,外部调用单例都不用创建
+ (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success failure:(nullable void (^)(NSError *_Nonnull error))failure;
@end
//
// NetworkTool.m
//
// Created by zhouyu on 16/10/25.
// Copyright © 2016年 mac All rights reserved.
//
#import "NetworkTool.h"
static NetworkTool *instance;
@implementation NetworkTool
+ (instancetype)shareNetworkTool {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[NetworkTool alloc]init];
// 让AFN默认也支持接收text/html文件类型
instance.responseSerializer.acceptableContentTypes
= [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
});
return instance;
}
//MARK: 一次封装的网络工具类
- (void)requestWithType:(requestType)type URLString:(NSString *)URLString parameters:(nullable id)parameters progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure{
if (type == requestTypeGET) {
[instance GET:URLString parameters:parameters progress:downloadProgress success:success failure:failure];
} else if (type == requestTypePOST) {
[instance POST:URLString parameters:parameters progress:downloadProgress success:success failure:failure];
}
}
//MARK: 二次封装的网络工具类
- (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success failure:(nullable void (^)(NSError *_Nonnull error))failure{
//(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
void (^orginalSuccess)(NSURLSessionDataTask * _Nullable, id _Nullable) = ^(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject) {
// success((NSDictionary *)responseObject);
success(responseObject);
};
//(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
void (^orginalFailure)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
};
[self requestWithType:type URLString:URLString parameters:parameters progress:nil success:orginalSuccess failure:orginalFailure];
}
//MARK: 三次封装的网络工具类
+ (void)requestWithType:(requestType)type URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(id _Nullable responseObject))success failure:(nullable void (^)(NSError *_Nonnull error))failure {
[[NetworkTool shareNetworkTool] requestWithType:type URLString:URLString parameters:parameters success:success failure:failure];
}
//网络请求
- (void)addNetworkData {
NSString *URLString = [NSString stringWithFormat:@"%@",BaseURLString];
NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
NSDictionary *parameters = @{
@"token":token
};
//使用第三次封装的类方法--类名直接调用,简洁明了
[NetworkTool requestWithType:requestTypePOST URLString:URLString parameters:parameters success:^(id _Nullable responseObject) {
ZYLog(@"%@",responseObject);
} failure:^(NSError *_Nonnull error) {
}];
}
ZYLog(@”%@”,responseObject);的输出结果
// MARK: 弹框
- (void)sendUIAlertControllerWithTitle:(NSString *)title message:(NSString *)message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}];
}
调用第一次封装的效果
[[NetworkTool shareNetworkTool] requestWithType:requestTypePOST URLString:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject) {
//处理返回的json
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self sendUIAlertControllerWithTitle:@"用户信息获取失败" message:@"网络延迟/系统繁忙"];
}];
调用第二次封装的效果
[[NetworkTool shareNetworkTool] requestWithType:requestTypePOST URLString:URLString parameters:parameters success:^(id _Nullable responseObject) {
//返回的数据处理
} failure:^(NSError * _Nonnull error) {
[self sendUIAlertControllerWithTitle:@"用户信息获取失败" message:@"网络延迟/系统繁忙"];
}];