较老版本 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")];

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

目录
相关文章
|
8月前
|
SQL PHP 数据库
19 PHP如何利用PDO获取结果集
路老师在知乎上分享了关于PHP语言的知识,帮助大家入门并深入了解PHP。本文介绍了PDO中获取结果集的三种方法:`fetch()`、`fetchAll()` 和 `fetchColumn()`,并通过具体案例展示了如何使用这些方法从数据库中获取数据并展示在网页上。
300 5
|
8月前
|
PHP
29 JpGraph图像绘制库
路老师在知乎上分享了PHP语言的知识,帮助大家入门并深入了解PHP。本文介绍了JpGraph库的下载、使用及中文乱码设置,通过实例展示了如何使用JpGraph绘制折线图。
94 2
|
Java API Nacos
SpringCloud Demo入门学习
SpringCloud Demo入门学习
120 0
|
人工智能 搜索推荐 API
[AI Perplexica] AI驱动的开源搜索引擎
探索Perplexica,一款由AI驱动的开源搜索引擎,了解其特点、使用方法以及如何安装。
[AI Perplexica] AI驱动的开源搜索引擎
|
机器学习/深度学习 存储 数据可视化
手把手教你绘制和解读实用R列线图(Nomogram):从入门到精通
手把手教你绘制和解读实用R列线图(Nomogram):从入门到精通
2640 1
|
Java API
Java的List,如何删除重复的元素,教你三个方法搞定!
Java的List,如何删除重复的元素,教你三个方法搞定!
513 0
|
监控 网络协议 安全
2023年最新整理的中兴设备命令合集,网络工程师收藏!
2023年最新整理的中兴设备命令合集,网络工程师收藏!
983 0
|
存储 Android开发 UED
Studio one6免费详细的下载安装教程
Studio One 6是一款非常专业的音乐创作编辑软件。为用户提供了所有一切你所需要创作的功能,包括所有的歌曲、项目、仪表板等动能,而且还自定义添加配置文件,良好的界面交互和丰富的功能板块。Studio One是一款DAW宿主音乐软件。用于创建,录制,混音和掌握音乐及其他音频,数字音频工作站软件Studio One,容易上手,将传统录音工作室的模式和现在的创造方式相结合从而带来舒适流畅的音乐创作体验。
3842 0
|
JSON 资源调度 安全
CCF-CSP认证历年题解
CCF-CSP认证历年题解
2374 1
CCF-CSP认证历年题解