较老版本的 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(@"[$10000000$] 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")];

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

较老版本 AFNetworking 使用心得的更多相关文章

  1. 较老版本的AFNetworking使用心得

    较老版本的 AFNetworking 下载链接 ( http://pan.baidu.com/s/14Cxga ) 将压缩包中的文件夹拖入xcode工程项目中并引入如下的框架 简单的 JOSN 解析例 ...

  2. 如何给不支持新特性的浏览器打补丁(让老版本IE兼容新特性)

    一个非常棒的 JavaScript 框架叫做 Modernizr(http://www.modernizr. com),用于向缺少 HTML5/CSS3特性支持的浏览器打补丁.由 Alexander ...

  3. 快速入门系列--WebAPI--04在老版本MVC4下的调整

    WebAPI是建立在MVC和WCF的基础上的,原来微软老是喜欢封装的很多,这次终于愿意将http编程模型的相关细节暴露给我们了.在之前的介绍中,基本上都基于.NET 4.5之后版本,其System.N ...

  4. 微软Hololens学院教程-Hologram 220-空间声音(Spatial sound )【本文是老版本,与最新的微软教程有出入】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦! 原文链接https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  5. 微软Hololens学院教程-Hologram 230-空间场景建模(Spatial mapping )【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  6. 微软Hololens学院教程- Holograms 100: Getting Started with Unity【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  7. 微软Hololens学院教程-Hologram 212-Voice(语音)【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  8. 微软Hololens学院教程-Hologram 211-Gestures(手势)【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  9. 微软Hololens学院教程-Hologram 210 Gaze(凝视)【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

随机推荐

  1. aria2

    在之前我们已经介绍了通过uGet使用aria2来进行下载,但是这样只是使用aria2最简单的功能,现在我们来介绍一下aria2的常用命令 简单篇: 一般使用使用 aria2 下载文件,只需在命令后附加 ...

  2. RSS新手必读

    当谷歌停止Google Reader后,我开始玩RSS Reader了.网上大抵说Google Reader的退出很可惜,不过替代品还是存在的.   作为一个newbie我的视野或许很局限不过还是说几 ...

  3. 打造一款便携版的Sublime Text

    https://segmentfault.com/a/1190000000707661 https://www.cnblogs.com/52cik/p/sublime-diy.html 直接安装Sub ...

  4. weex官方demo weex-hackernews代码解读(上)

    一.介绍 weex 是阿里出品的一个类似RN的框架,可以使用前端技术来开发移动应用,实现一份代码支持H5,IOS和Android.最新版本的weex已默认将vue.js作为前端框架,而weex-hac ...

  5. Javascript中DOM详解与学习

    DOM(文档对象模型)是针对html和XML文档的一个API(应用程序编程接口).DOM描绘了一个层次化的节点树,允许开发人员添加,移除和修改页面的某一部分.下面将从这几个层次来学习. 一.节点层次 ...

  6. Python3 决策树ID3算法实现

    # -*- coding: utf-8 -*- """ Created on Wed Jan 24 19:01:40 2018 @author: markli 采用信息增 ...

  7. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  8. vs 2010 :类型化数据集DataSet应用

    1.启动服务器资源管理器,建立数据库连接 2.在项目中创建数据集 3.为数据集添加表对象 4.为表适配器tableAdapter添加参数化查询 5.修改表适配器的主查询,或添加其他查询 Update: ...

  9. ueditor上传图片设置的简单实例

    0.前言:我用过ckeditor,kingeditor还是感觉ueditor最好用,功能强大,经常更新.之前因为升级了struts2到2.5的了,原本的kingeditor已经不能共存,于是找到了ud ...

  10. CentOS 7设置KVM硬盘模式为SCSI

    找到一下节点,把target节点的dev改成s开头,bus改成scsi即可,并删除address节点: 以此内推,如果要修改为ide需要修改dev为h开头,bus改成ide. 参考: https:// ...