• iOS---对AFNetworking进行二次封装,使用单例单独处理网络请求数据


    //在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
  • 相关阅读:
    等差子序列(sequence)
    威士忌(whiskey)
    图论:2-SAT模板
    poj2723-Get Luffy Out
    acdream1412:2-3 trees 组合数dp
    hdu3849-By Recognizing These Guys, We Find Social Networks Useful:双连通分量
    ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度
    ZOJ3519-Beautiful People:最长上升子序列的变形
    hdu2460-Network:边的双连通分量
    hdu4405:概率dp
  • 原文地址:https://www.cnblogs.com/zhangshan/p/5313010.html
Copyright © 2020-2023  润新知