2 添加類庫 Security.framework、MobileCoreServices.framework、SystemConfiguration.frameworkphp
和+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 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@N01&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.圖片上傳處理,監測上傳狀態:
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 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];
from :http://www.zpluz.com/forum.php?mod=viewthread&tid=2052
AFNetWorking的JSON解析默認庫是使用的AFJSONRequestOperation模式,只支持text/json,application/json,text/javascript,因此若是出現code=-1016錯誤則說明當前的JSON解析模式是text/html,因此要加上這段代碼:app