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

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

标签:  iOS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值