AFNetworking 基本使用

  1. AFNetwork是一个轻量级的网络请求api类库。是以NSURLConnection, NSOperation和其他方法为基础的。
  2. 下面这个例子是用来处理json请求的
3如何选择AFNetworking版本

首先得下载AFNetworking库文件,下载时得首先弄清楚,你将要开发的软件兼容的最低版本是多少。AFNetworking 2.0或者之后的版本需要xcode5.0版本并且只能为IOS6或更高的手机系统上运行,如果开发MAC程序,那么2.0版本只能在MAC OS X 10.8或者更高的版本上运行。

如果你想要兼容IOS5或MAC OS X 10.7,那你需要用最新发布的1.x版本
如果要兼容4.3或者MAC OS X 10.6,需要用最新发布的0.10.x版本
 官网下载2.5版本:http://afnetworking.com/
2013年大多数软件兼容的最低版本为4.3,而2014年,估计大多数软件兼容的最低版本将会是5.0甚至6.0;
所以,目前最好的选择还是1.x版本,兼容到IOS5.0。

接下来就是在头文件要引入    

#import "AFNetworking.h"

 然后

- (void)viewDidLoad {

    NSString *urlString = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/feed.bak?num=10&date=%f",[self getdate]];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSString *html = operation.responseString;

        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];

        NSDictionary *dict = [NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];

      //自己解析的话就在这里开始解析自己的内容就可以了

        self.dataArr = [NSMutableArray new];

        self.videoArray = [NSMutableArray new];

        for (NSDictionary *dailyList in dict[@"dailyList"])

        {

            NSString *key = dailyList[@"date"];

            NSMutableArray *tempArr = [NSMutableArray new];

            for (NSDictionary *videoModel in dailyList[@"videoList"]) {

                FZMenuModel *model = [[FZMenuModel alloc]init];

                [model setValuesForKeysWithDictionary:videoModel];

                [tempArr addObject:model];

            }

            [self.keysArr addObject:key];

            [self.datadit setValue:tempArr forKey:key];

        }

        [self.keysArr sortUsingSelector:@selector(compare:)];

        //结束解析

        //刷新(一个是在controller里面自己定义的View,另一个是在自己定义的View里面又定义的一个tableview)

        [self.fzmenuview.maintableview reloadData];

        NSLog(@"%@",dict);

    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"发生错误!%@",error);

    }];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [queue addOperation:operation];

}

//时间供上面网址调用

-(NSTimeInterval )getdate{

    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

    NSTimeInterval a = [dat timeIntervalSince1970]* 1000;

    return a;

}

    

原文地址:https://www.cnblogs.com/FZP5/p/4964149.html