AFNetWorking能做什麼

來源:互聯網
上載者:User

標籤:mutable   end   img   down   nil   轉化   應用程式   imp   expec   

AFNetwork是一個輕量級的網路請求api類庫。是以NSURLConnection, NSOperation和其它方法為基礎的。以下這個範例是用來處理json請求的: NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {     NSLog(@"App.net Global Stream: %@", JSON); } failure:nil]; [operation start]; 用法: 1.下載AFNetwork, 點擊下載 2.將目錄名稱為AFNetworking拖入到你的工程項目就可以 常見問題: 1.  AFNetworking作用都有哪些? NSURLConnection 提供了+sendAsynchronousRequest:queue:completionHandler: 和+sendAsynchronousRequest:queue:completionHandler: ,可是AFNetworking提供了更好的功能 *AFURLConnectionOperation和它的子類繼承NSOperation的,同意請求被取消,暫停/恢複和由NSOperationQueue進行管理。

*AFURLConnectionOperation也能夠讓你輕鬆得完畢上傳和下載,處理驗證。監控上傳和下載進度,控制的緩衝。

*AFHTTPRequestOperation和它得子類能夠基於http狀態和 內容列下來區分是否成功請求了 *AFNetworking能夠將遠程媒體資料類型(NSData)轉化為可用的格式。比方如JSON,XML,映像和plist。

*AFHTTPClient提供了一個方便的網路互動介面,包含預設頭,身分識別驗證。是否串連到網路,批量處理操作。查詢字串參數序列化。已經多種表單請求 *的UIImageView+ AFNetworking添加了一個方便的方法來非同步載入映像。 2. AFNetworking是否支援緩衝? 能夠。NSURLCache及其子類提供了非常多進階介面用於處理緩衝 假設你想將緩衝儲存再磁碟,推薦使用SDURLCache 3.怎樣使用AFNetworking上傳一個檔案? NSData *imageData = UIImagePNGRepresentation(image); NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData mimeType:@"image/png" name:@"avatar"]; }]; 4.怎樣使用AFNetworking下載一個檔案? 先建立一個 AFURLConnectionOperation對象,然後再使用它的屬性 outputStream進行處理 operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO]; 5.怎樣解決:SystemConfiguration framework not found in project 請匯入: #import <SystemConfiguration/SystemConfiguration.h> #import <MobileCoreServices/MobileCoreServices.h> 6.當應用程式退出時。怎樣保持持續的請求? AFURLConnectionOperation有一個叫 setShouldExecuteAsBackgroundTaskWithExpirationHandler:的方法用於處理在應用程式進入後台後,進行持續的請求 [self setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ }]; 一些執行個體: 1.XML 請求 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]; 2.圖片請求: 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"]]; 3.圖片上傳處理,監測上傳狀態: didiwei 17:28:57 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]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; [operation start]; 4.線上流媒體請求 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]]; operation.outputStream = [NSOutputStream outputStreamToMemory]; [operation start];


AFNetWorking能做什麼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.