较老版本 AFNetworking 使用心得

简介:

较老版本的 AFNetworking 下载链接 http://pan.baidu.com/s/14Cxga

将压缩包中的文件夹拖入xcode工程项目中并引入如下的框架

 

简单的 JOSN 解析例子
    static NSString *serverAddress = @"http://m.weather.com.cn/data/101110101.html";

  // 1.创建JSON操作对象
    AFJSONRequestOperation *operation =
    [AFJSONRequestOperation
     JSONRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:serverAddress]]
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
         NSLog(@"success -- %@", JSON);
     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
         NSLog(@"failure -- %@", JSON);
     }];

 

    // 2.执行对象的操作异步加载
    [operation start];

 

简单的 XML 解析例子

    static NSString *serverAddress = @"http://flash.weather.com.cn/wmaps/xml/beijing.xml";
    
    // 1.创建XML操作对象
    AFXMLRequestOperation *operation =
    [AFXMLRequestOperation
     XMLParserRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:serverAddress]]
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
         NSLog(@"success -- %@", XMLParser);
     }
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
         NSLog(@"failure -- %@", XMLParser);
     }];


    // 2.执行对象的操作异步加载
    [operation start];

HTTP POST请求例子

-----------------------------------------------------------------------------------------------------

//内联函数

NS_INLINE AFHTTPClient * createAFHTTPClient(NSString *baseURLString)
{
    //创建一个AFHTTPClient的链接,仅需传入服务器URL的String即可
    return [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:baseURLString]];
}

NS_INLINE NSData * createJSONDataFromDict(NSDictionary *params)
{
    //根据字典创建出JSON专用格式的NSData
    return [NSJSONSerialization dataWithJSONObject:params
                                           options:NSJSONWritingPrettyPrinted
                                             error:nil];
}

-----------------------------------------------------------------------------------------------------

//服务器地址
    static NSString *serverAddress = @"http://art.wooboo.com.cn/support/service.shtml";
    
    //初始化一个本地的httpClient
    AFHTTPClient *httpClient = createAFHTTPClient(serverAddress);
    
    //完善httpClient并形成一个POST请求报文
    NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                  path:serverAddress
                                                            parameters:nil
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        
        NSArray *paramsType = @[@{@"action": @"loadImg", @"artId": @"0"}];
        
        
        //转换字典数据为JSON专用格式并再次转换为字符串
        NSString *params = [[NSString alloc] initWithData:
                            createJSONDataFromDict(paramsType[0])
                                                 encoding:NSUTF8StringEncoding];
        
        
        //进一步完善请求的内容 (Content-Disposition: form-data; name=#{name}")
        [formData appendPartWithFormData:[params dataUsingEncoding:NSUTF8StringEncoding]
                                    name:@"p"];
    }];
    
    
    //将请求报文发送到服务器进行链接
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation
     setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
         
         
         NSLog(@"%@", jsonObjectFromData(responseObject));
         
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         
         NSLog(@"error.");
         
     }];
    [operation start];

加载网络图片

-----------------------------------------------------------------------------------------------------

//内联函数

NS_INLINE NSURL * netURL(NSString *netPath)
{
    //网络文件的URL
    return [NSURL URLWithString:netPath];
}


NS_INLINE UIImage * imageFromBuddleByName(NSString *imageName)
{
    //通过名字获取buddle中图片资源
    return [UIImage imageNamed:imageName];
}

-----------------------------------------------------------------------------------------------------

- (void)setImageWithURL:(NSURL *)url
    placeholderImage:(UIImage *)placeholderImage;

Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.

-----------------------------------------------------------------------------------------------------

    static NSString *picServerAddress =
    @"http://wallpapers.wallbase.cc/high-resolution/wallpaper-2677423.jpg";

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];


    [imageView setImageWithURL:netURL(picServerAddress)
                      placeholderImage:imageFromBuddleByName(@"1.png")];

-----------------------------------------------------------------------------------------------------

目录
相关文章
|
Web App开发 开发工具 git
WebRTC 源码旧版本下载
WebRTC 源码旧版本下载
562 0
|
JSON PHP 数据格式
Composer 镜像原理 (3) —— 完结篇
Composer 是一个 PHP 的依赖管理工具,它可以帮助开发者轻松地管理和维护 PHP 项目中的依赖关系。你是否好奇过它的镜像仓库是怎么实现的?本文为你揭晓。
75 0
|
图形学
Rhino中文名称犀牛,是一款超强的三维建模工具Rhino 7.9安装教程(所有版本下载安装教程)
Rhino中文名称犀牛,是一款超强的三维建模工具。不过不要小瞧它,它包含了所有的NURBS建模功能,用它建模感觉非常流畅,所以大家经常用它来建模,然后导出高精度模型给其他三维软件使用。
|
移动开发 前端开发 小程序
为了偷懒,我用google/zx一键自动打包编译了前后端项目并发布到指定环境
由于正在负责的一个项目,就说前端涉及到PC端、公众号端、APP端的H5、小程序端、可视化大屏端,而PC和APP又通过qiankun引入了微前端的理念。整体一圈下来可能光前端编译打包就要build差不多二十次。而有时候经常性的bug改动,这个时候便只需要进行测试后需要进行小范围的测试。
210 0
|
Java API 开发工具
Magisk 最新版本 V24.1 源码编译踩坑集锦
Magisk 最新版本 V24.1 源码编译踩坑集锦
599 0
|
Web App开发 存储 网络协议
ios 组件化之Cocoapods私有库详解以及问题解决方案
ios 组件化之Cocoapods私有库详解以及问题解决方案
ios 组件化之Cocoapods私有库详解以及问题解决方案
|
存储 Linux 文件存储
After Effects2023最新版本在哪里可以下载?好不好用
AE也就是After Effects,软件的全称是Adobe After Effects 2023。是由Adobe Systems开发和发行的影视特效处理软件。
155 0
|
监控 安全 搜索推荐
全新版本CleanMyMac2023下载及有哪些新功能?
从最初开始下载CleanMyMac开始,CMM一直在提供智能的清理,从颇受小白用户喜爱的自动清理特性开始,仅需一键即可快速而安全地清理系统各角落垃圾,释放宝贵硬盘空间,CMM或许可以说是是 Mac上最知名的系统清理工具了。CleanMyMac2023下载:http://t.csdn.cn/vMNV9
108 0
|
自然语言处理 监控 安全
CleanMyMac2023官方最新版有哪些功能介绍?
电脑系统垃圾太多,影响使用效率,有哪些好用的mac系统垃圾清理工具值得入手呢?小编整理的几款简单好用的分享给大家,需要的拿走,小白必备,mac系统垃圾清理工具分享。
127 0