網(wǎng)絡(luò)請(qǐng)求工具類的封裝

使用的三方框架:pod 'AFNetworking', '~> 3.0'
繼承AFHTTPSessionManager的類
官方注釋:(英文很重要!!!)

## Subclassing Notes

 Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass `AFHTTPSessionManager`, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.

 For developers targeting iOS 6 or Mac OS X 10.8 or earlier, `AFHTTPRequestOperationManager` may be used to similar effect.

h文件

//  Created by Mac on 2017/8/29.
//  Copyright ? 2017年 Mac. All rights reserved.
//

#import <AFNetworking/AFNetworking.h>

@interface WJNetworkTools : AFHTTPSessionManager
+ (instancetype)sharedManager;
@end

m文件

//  Created by Mac on 2017/8/29.
//  Copyright ? 2017年 Mac. All rights reserved.
//

#import "WJNetworkTools.h"

@implementation WJNetworkTools
+ (instancetype)sharedManager{
    
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURL * baseURL = [NSURL URLWithString:@"https://abcdefg1234567/index/"];
        NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
        //配置超時(shí)時(shí)長(zhǎng)
        config.timeoutIntervalForRequest = 15;
        instance = [[self alloc]initWithBaseURL: baseURL sessionConfiguration:config];
    });
    return instance;
}

@end

JSON數(shù)據(jù)結(jié)構(gòu):


Jietu20170829-171833.png

創(chuàng)建數(shù)據(jù)模型:
h文件

//  Created by Mac on 2017/8/29.
//  Copyright ? 2017年 Mac. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface WJModel : NSObject
@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * describe;

+ (instancetype)wangjieWithDic:(NSDictionary *)dic;
//發(fā)送異步請(qǐng)求,獲取數(shù)據(jù),字典轉(zhuǎn)模型
+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error;


@end

m文件

//  Created by Mac on 2017/8/29.
//  Copyright ? 2017年 Mac. All rights reserved.
//

#import "WJModel.h"
#import "WJNetworkTools.h"
@implementation WJModel

+ (instancetype)wangjieWithDic:(NSDictionary *)dic{
    WJModel * model = [self new];
    [model setValuesForKeysWithDictionary:dic];
    return model;
}


//防止請(qǐng)求到的字段多于你定義的字段
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

}


//發(fā)送異步請(qǐng)求,獲取數(shù)據(jù),字典轉(zhuǎn)模型
+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error{

    [[WJNetworkTools sharedManager] GET:@"new_list.php?os=0&index=news&limit=1&token=4f09ef8d44138f3c5e4b215a783d47dd" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  responseObject) {
        NSLog(@"請(qǐng)求成功");
        //獲取返回的數(shù)組
        //獲取字典的第一個(gè)鍵
        //NSString * rootKey = responseObject.keyEnumerator.nextObject;
        NSArray * array = responseObject[@"data"];
        //字典轉(zhuǎn)模型
        NSMutableArray * mArray = [NSMutableArray arrayWithCapacity:4];
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            WJModel * model = [self wangjieWithDic:obj];
            [mArray addObject:model];
        }];
        
        if (success) {
            success(mArray.copy);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull err) {
        
        NSLog(@"請(qǐng)求失敗%@",error);
        if (error) {
            error();
        }
    }];
}


//重寫系統(tǒng)description(有的重寫系統(tǒng)方法需要 super ,有的不需要,系統(tǒng)默認(rèn)super要super,沒有super的就不super)
-(NSString *)description{
    return [NSString stringWithFormat:@"%@{name:%@,age:%@}",[super description],self.title,self.describe];
}

ViewController調(diào)用

//  Created by Mac on 2017/8/29.
//  Copyright ? 2017年 Mac. All rights reserved.
//

#import "ViewController.h"
#import "WJModel.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [WJModel wangjie:^(NSArray *array) {
        NSLog(@"array--%@",array);
    } error:^{
        
    }];
   
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,180評(píng)論 4 61
  • 最近的朋友圈被《人民的名義》刷爆了,作為每天的必修課,晚上新聞聯(lián)播之后立馬鎖定湖南衛(wèi)視收看最新的兩級(jí)。我也加入了追...
    悟成閱讀 888評(píng)論 1 49
  • 午覺醒來,全身酸痛,哦,是昨天跑步的后遺癥,累,乏力,7月15日十點(diǎn)三十分,結(jié)束了急診一周的見習(xí),很無聊,很尷尬,...
    兔子要吃胡蘿卜呀閱讀 165評(píng)論 0 0
  • 桉術(shù)CRM:?jiǎn)渭儗RM作為銷售跟蹤工具的日子已經(jīng)過去,CRM的未來是進(jìn)行預(yù)測(cè)分析、加深對(duì)某一行業(yè)的理解、為工作流...
    桉術(shù)CRM閱讀 621評(píng)論 0 5
  • 安靜的地方總是遠(yuǎn)離鬧市,美麗的風(fēng)景總是路途遙遠(yuǎn),但那些地方,那些風(fēng)景往往最能觸動(dòng)心弦,讓人歡喜。一直以來都...
    誰家的梅子閱讀 510評(píng)論 0 4