//在DataManager.h里 #import <Foundation/Foundation.h> @class DataManager; //block回调传值 /** * 请求成功回调json数据 * * @param json json串 */ typedef void(^Success)(id json); /** * 请求失败回调错误信息 * * @param error error错误信息 */ typedef void(^Failure)(NSError *error); @interface DataManager : NSObject //单例模式 + (DataManager *)manager; /** * GET请求 * * @param url NSString 请求url * @param paramters NSDictionary 参数 * @param success void(^Success)(id json)回调 * @param failure void(^Failure)(NSError *error)回调 */ - (void)getDataWithUrl:(NSString *)url parameters:(NSDictionary *)paramters success:(Success)success failure:(Failure)failure; /** * POST请求 * * @param url NSString 请求url * @param paramters NSDictionary 参数 * @param success void(^Success)(id json)回调 * @param failure void(^Failure)(NSError *error)回调 */ - (void)postDataWithUrl:(NSString *)url parameters:(NSDictionary *)paramters success:(Success)success failure:(Failure)failure; @end
//在DataManager.m里 #import "DataManager.h" #import <AFNetworking.h> #import <JSONKit.h> #import <UIKit+AFNetworking.h> static DataManager *manager = nil; static AFHTTPSessionManager *afnManager = nil; @implementation DataManager /** * 创建单例 * * @return <#return value description#> */ + (DataManager *)manager { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ manager = [[DataManager alloc] init]; afnManager = [AFHTTPSessionManager manager]; afnManager.responseSerializer = [AFHTTPResponseSerializer serializer]; }); return manager; } /** * 处理字符串将其转成标准json格式 * * @param data 响应数据 * * @return id */ + (id)handleResponseObject:(NSData *)data { //将获取的二进制数据转成字符串 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //去掉字符串里的转义字符 NSString *str1 = [str stringByReplacingOccurrencesOfString:@"\" withString:@""]; //去掉头和尾的引号“” NSString *str2 = [str1 substringWithRange:NSMakeRange(1, str1.length-2)]; //最终str2为json格式的字符串,将其转成需要的字典和数组 id object = [str2 objectFromJSONString]; return object; } /** * 提示信息 * * @param message 要提示的内容 */ + (void)showAlertViewWithMessage:(NSString *)message { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertView show]; } //GET请求 - (void)getDataWithUrl:(NSString *)url parameters:(NSDictionary *)paramters success:(Success)success failure:(Failure)failure { afnManager.responseSerializer = [AFJSONResponseSerializer serializer]; [afnManager GET:url parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (responseObject) { success(responseObject); }else { [[self class] showAlertViewWithMessage:@"暂无数据"]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (error) { [[self class] showAlertViewWithMessage:@"服务器出错了~~~~(>_<)~~~~"]; failure(error); } }]; } //POST请求 - (void)postDataWithUrl:(NSString *)url parameters:(NSDictionary *)paramters success:(Success)success failure:(Failure)failure { [afnManager POST:url parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (responseObject) { success([[self class] handleResponseObject:responseObject]); }else { [[self class] showAlertViewWithMessage:@"暂无数据"]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (error) { [[self class] showAlertViewWithMessage:@"服务器出错了~~~~(>_<)~~~~"]; failure(error); } }]; } @end