zoukankan      html  css  js  c++  java
  • ios开发之--使用AFNetWorking 3.1.0 ,简单的请求封装类

    从苹果系统自带的请求类,到ASIHttpRequest第三方请求类,再到AFNetWorking第三方请求类,目前只要牵扯到数据请求,基本上都是用AFN,所以,这里纯粹是记录下:

    hRequestTools.h

    #import <Foundation/Foundation.h>
    
    /**
     *  网络请求类型
     */
    typedef NS_ENUM(NSUInteger,HttpRequestType) {
        /**
         *  get请求
         */
        HttpRequestTypeGet = 1,
        /**
         *  post请求
         */
        HttpRequestTypePost = 2
    };
    
    @interface hRequestTools : NSObject
    
    
    /**
     *  发送网络请求
     *
     *  @param URLString   请求的网址字符串
     *  @param parameters  请求的参数
     *  @param type        请求的类型
     *  @param success     请求的结果
     */
    + (void)requestWithURLString:(NSString *)URLString
                      parameters:(id)parameters
                            type:(HttpRequestType)type
                         success:(void (^)(id responseObject))success
                         failure:(void (^)(NSError *error))failure;

    hRequestTools.m

    #pragma mark 封装的请求方法
    + (void)requestWithURLString:(NSString *)URLString
                      parameters:(id)parameters
                            type:(HttpRequestType)type
                         success:(void (^)(id responseObject))success
                         failure:(void (^)(NSError *error))failure
    {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.requestSerializer.timeoutInterval = 30.0f;
        switch (type) {
            case HttpRequestTypeGet:
            {
                [manager GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
                    
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    
                    if (success) {
                        id jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers  error:nil];
                        success(jsons);
                    }
                    
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    if (failure) {
                        failure(error);
                    }
                }];

    }
    break; case HttpRequestTypePost: { [manager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
                    
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    
                    if (success) {
                        id jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers  error:nil];
                        success(jsons);
                    }
                    
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    
                    if (failure) {
                        failure(error);
                    }
                    
                }];
    }
    break; default: break; } }

    请求里面如果要加缓存的话,可以和YYCache这个第三方配合使用,

    关于YYCache,可以推荐几个链接:http://www.jianshu.com/p/b8dcf6634fab

  • 相关阅读:
    Java小白集合源码的学习系列:Vector
    Java小白集合源码的学习系列:LinkedList
    707.设计双向链表
    Java小白集合源码的学习系列:ArrayList
    Leetcode动态规划【简单题】
    小白学Java:老师!泛型我懂了!
    小白学Java:包装类
    Java面向对象之异常详解
    Java面向对象之异常【一】
    浅谈Java中接口与抽象类的异同
  • 原文地址:https://www.cnblogs.com/hero11223/p/6757415.html
Copyright © 2011-2022 走看看