AFNetworking教程

轉:http://www.lanrenios.com/tutorials/network/2012/1126/527.htmlhtml

AFNETWORKING

AFNetworking他是一個如今很是用得多的ios開發中網絡開源庫,它是很是的討人喜歡的網絡庫,適用於iOS以及Mac OS X. 它構建於在(apple ios開發文檔)NSURLConnectionNSOperation,
以及其餘熟悉的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];

如何開始使用AFNETWORKING

CORE:

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數據.異步

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];
相關文章
相關標籤/搜索