转:http://www.lanrenios.com/tutorials/network/2012/1126/527.htmlhtml
AFNetworking他是一个如今很是用得多的ios开发中网络开源库,它是很是的讨人喜欢的网络库,适用于iOS以及Mac OS X. 它构建于在(apple ios开发文档)NSURLConnection, NSOperation,
以及其余熟悉的Foundation技术之上. 它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来很是轻松.
下面是一个获得json实例,他能够使用很轻松的方式从一个url来获得json数据:ios
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]; |
AFURLConnectionOperation:一个 NSOperation 实现了NSURLConnection 的代理方法.json
HTTP Requests:api
AFHTTPRequestOperation:AFURLConnectionOperation的子类,当request使用的协议为HTTP和HTTPS时,它压缩了用于决定request是否成功的状态码和内容类型.网络
AFJSONRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理jason response数据.架构
AFXMLRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理xml response数据.app
AFPropertyListRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理property list response数据.异步
AFHTTPClient:捕获一个基于http协议的网络应用程序的公共交流模式.包含:模块化
AFImageRequestOperation:一个AFHTTPRequestOperation的子类,用于下载和处理图片.测试
UIImageView+AFNetworking:添加一些方法到UIImageView中,为了从一个URL中异步加载远程图片
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]; |
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"]]; |
// 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]; |
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]; |
art]; |