http://cocoadocs.org/docsets/AFNetworking/2.5.0/html
AFNetworking的用法ios
提交GET請求和POST請求web
AFNetworking是第三方框架,閱者自行去官網上下載、安裝。json
1>建立AFHTTPRequestOpeartionManger對象api
2>根據服務器內容不一樣,爲AFHTTPRequestOpeartionManger對象指定不一樣的解析器,該對象默認的解析器是JSON和plist文件解析器。服務器
3>發送GET請求,用Manager對象調用GET方法便可,success代碼塊和failure代碼塊是在網絡請求成功/失敗後調用。網絡
4>success參數指定了代碼塊中處理服務器響應成功的正確數據;failure參數指定了代碼塊中處理服務器響應失敗的錯誤數據。app
AFHTTPRquestOperationManager
框架
包含了常見的HTTP訪問web站點的模式,有建立請求、鏈接響應、網絡類型監視等。url
「GET」請求格式:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //調用get方法 [manager GET:@「http://example.com/resources.json」 parameters:parameters //加載成功的代碼塊 success:^(AFHTTPRequestOperation *operation,id responseObject) { NSLog(@"JSON:%@",responseObject); } //加載失敗的代碼塊 failure:^(AFHTTPRequestOperation *operation,NSError *error) { NSLog(@"Error:%@",error); }];
"POST"請求格式:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"foo": @"bar"}; [manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
"POST"多個請求
//建立對象 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //請求內容 NSDictionary *parameters = @{@"foo": @"bar"}; //請求地址 NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; [manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileURL:filePath name:@"image" error:nil]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
2.建立一個下載文件任務
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"File downloaded to: %@", filePath); }]; [downloadTask resume];
3.建立一個跟新(上傳)文件任務
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"Error: %@", error); } else { NSLog(@"Success: %@ %@", response, responseObject); } }]; [uploadTask resume];
4.處理JSON或plist響應
IOS應用在處理JSON和Plist響應的時候能夠將其轉換成NSDictionary對象或是NSArray對象,AFHTTPRequestOperationManager默承認以處理JSON或plist響應,所以當響應內容默認爲JSON時,無需再次指定服務器響應解析器。
NSDictionary *temDic = [[NSDictionary alloc] init]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSString *url = @"http://api.map.baidu.com/telematics/v3/weather?"; NSDictionary *parameters = @{@"location":@"長沙",@"output":@"json",@"ok":@"akljhgffg"}; [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation,id responseObject) { NSLog(@"JSON:%@",responseObject); temDic = responseObject; }failure:^(AFHTTPRequestOperation *operation,NSError *error) { NSLog(@"Error:%@",error); }];
http://www.cocoachina.com/ios/20141120/10265.html