转:http://www.lanrenios.com/tutorials/network/2012/1126/527.html

AFNETWORKING

AFNetworking他是一个现在非常用得多的ios开发中网络开源库,它是非常的讨人喜欢的网络库,适用于iOS以及Mac OS X. 它构建于在(apple ios开发文档)NSURLConnectionNSOperation,

以及其他熟悉的Foundation技术之上. 它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松.

下面是一个得到json实例,他可以使用很轻松的方式从一个url来得到json数据:

 
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"];
 
NSURLRequest *request = [NSURLRequest requestWithURL:url];
 
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
 
    NSLog(@"Public Timeline: %@", JSON);
 
} failure:nil];
 
[operation start];

如何开始使用AFNETWORKING

CORE:

AFURLConnectionOperation:一个 NSOperation 实现了NSURLConnection 的代理方法.

HTTP Requests:

AFHTTPRequestOperation:AFURLConnectionOperation的子类,当request使用的协议为HTTP和HTTPS时,它压缩了用于决定request是否成功的状态码和内容类型.

AFJSONRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理jason response数据.

AFXMLRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理xml response数据.

AFPropertyListRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理property
list
 response数据.

HTTP CLIENT:

AFHTTPClient:捕获一个基于http协议的网络应用程序的公共交流模式.包含:

  • 使用基本的url相关路径来只做request
  • 为request自动添加设置http headers.
  • 使用http 基础证书或者OAuth来验证request
  • 为由client制作的requests管理一个NSOperationQueue
  • 从NSDictionary生成一个查询字符串或http bodies.
  • 从request中构建多部件
  • 自动的解析http response数据为相应的表现数据
  • 在网络可达性测试用监控和响应变化.

IMAGES

AFImageRequestOperation:一个AFHTTPRequestOperation的子类,用于下载和处理图片.

UIImageView+AFNetworking:添加一些方法到UIImageView中,为了从一个URL中异步加载远程图片

例子程序

XML REQUEST

 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]];
 
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
 
  XMLParser.delegate = self;
 
  [XMLParser parse];
 
} failure:nil];
 
[operation start];

IMAGE REQUEST

 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
 
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];

API CLIENT REQUEST

 
// AFGowallaAPIClient is a subclass of AFHTTPClient, which defines the base URL and default HTTP headers for NSURLRequests it creates
 
[[AFGowallaAPIClient sharedClient] getPath:@"/spots/9223" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
 
    NSLog(@"Name: %@", [responseObject valueForKeyPath:@"name"]);
 
    NSLog(@"Address: %@", [responseObject valueForKeyPath:@"address.street_address"]);
 
} failure:nil];

FILE UPLOAD WITH PROGRESS CALLBACK

 
NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
 
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
 
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
 
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
 
}];
 
 
 
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
 
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
 
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
 
}];
 
[operation start];

STREAMING REQUEST

 
art];

AFNetworking教程的更多相关文章

  1. AFNetworking 2.0教程

    在iOS 7中,Apple更新了iOS中的网络基础架构,新推出的网络基础架构是NSURLSession(原来的网络基础架构NSURLConnection). iOS开发中往往会涉及网络数据处理,像其他 ...

  2. AFNetworking速成教程

    转:http://palmsky.net/?p=4138 本文是由 iOS Tutorial 小组成员 Scott Sherwood撰写,他是一个基于位置动态加载(Dynamically Loaded ...

  3. AFNetworking 系列教程

      前几天发现iOS9.xcode7之后NSURLConnection的API被弃用了,取而代之的是NSURLSession,看到这我首先想到了AFNetworking,因为工程的所有网络请求都是基于 ...

  4. ios cocopods 安装使用及高级教程

    CocoaPods简介 每种语言发展到一个阶段,就会出现相应的依赖管理工具,例如Java语言的Maven,nodejs的npm.随着iOS开发者的增多,业界也出现了为iOS程序提供依赖管理的工具,它的 ...

  5. [转]CocoaPods安装和使用教程

    转载地址:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用Coco ...

  6. << CocoaPods安装和使用教程 >>github code4app以及cocoachina 苹果官方文档

    developer.apple.com 英文搜索各个技术的官方介绍文档, 前提是英文过关 cocoachina ios最新新闻, 信息 code4app上有许多组件 http://www.code4a ...

  7. 史上最详细的CocoaPods安装教程

    虽然网上关于CocoaPods安装教程多不胜数,但是我在安装的过程中还是出现了很多错误,所以大家可以照下来步骤装一下,我相信会很好用. 前言 在iOS项目中使用第三方类库可以说是非常常见的事,但是要正 ...

  8. CocoaPods安装和使用教程(转摘)

    Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装Co ...

  9. CocoaPods安装和使用教程

    Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装Co ...

随机推荐

  1. 《head first java 》读书笔记(四)

    Updated 2014/04/09 P518--P581 <数据结构> ArrayList不能排序:TreeSet以有序状态保持并可防止重复.HashMap可用成对的name/value ...

  2. 140304笔记, mysql 更改自动增长字段值的重新设定

    1. 存在同名的或不同的namespace交错情况. Caused by: com.ibatis.sqlmap.client.SqlMapException: There is no result m ...

  3. DevOps 和技术债务偿还自动化

    当企业想要迁移到一个 DevOps 模型时,经常需要偿还高等级的技术债务 说得更明确一点,机构往往陷入「技术债务的恶性循环」中,以至于任何迅速.敏捷的迁移方式都无法使用.这是技术债务中的希腊债务危机水 ...

  4. POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)

    题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...

  5. POJ 1716

    #include <iostream> #include <algorithm> #define MAXN 20005 using namespace std; int _m[ ...

  6. DIV+CSS列表式布局(同意图片的应用)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. jquery控制按钮的禁用与启用

    jquery禁用a标签方法1: $(document).ready(function () { $("a").each(function () { var textValue = ...

  8. cygwin如何断点续传

    对于Cygwin,如果想安装的东西比较多的话,推荐先选择“Download without installing”,下载完了再从本地安装. 好了,说关于断点续传.我所知道的是—— 网上有说法:下载失败 ...

  9. 261. Graph Valid Tree

    题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...

  10. 54. Spiral Matrix

    题目: Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral ...