AFN的下載地址
https://github.com/AFNetworking/AFNetworking
下面也有具體的使用方法
下載好解壓,把AFNetworking文件夾拖入項目
引入頭文件
#import "AFNetworking.h"
get請求
//創建一個管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//設置服務器端返回的數據格式,默認爲JSON解析AFJSONResponseSerializer AFXMLResponseSerializer AFResponseSerializer
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
設置get請求後面的參數
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"pwd"] = @"123";
//設置url
NSString *string = @"http://localhost:8080/TFServer/login";
[mgr GET:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//獲取成功以後執行這個代碼塊
NSLog(@"---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失敗執行的代碼
NSLog(@"error");
}];
post請求
//創建一個管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//設置服務器端返回的數據格式,默認爲JSON解析AFJSONResponseSerializer AFXMLResponseSerializer AFResponseSerializer
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
設置get請求後面的參數
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"pwd"] = @"123";
//設置url
NSString *string = @"http://localhost:8080/TFServer/login";
//只是這裏把get變爲POST
[mgr POST:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//獲取成功以後執行這個代碼塊
NSLog(@"---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失敗執行的代碼
NSLog(@"error");
}];
上傳文件
新式的方法,使用與iOS7
//得到文件的路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"1.jpg" ofType:nil];
//創建一個request對像
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost:8080/TFServer/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"file" fileName:@"test.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
//創建一個manager對象
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
//創建任務
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
上傳方法1
將上傳的文件轉換爲data數據然後上傳
// 1.創建一個管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封裝參數(這個字典只能放非文件參數)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"age"] = @20;
params[@"pwd"] = @"456";
params[@"height"] = @1.55;
// 2.發送一個請求
NSString *url = @"http://localhost:8080/TFServer/upload";
[mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
UIImage *image = [UIImage imageNamed:@"1.jpg"];
NSData *fileData = UIImageJPEGRepresentation(image, 1.0);
[formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.jpg" mimeType:@"image/jpeg"];
// 不是用這個方法來設置文件參數
// [formData appendPartWithFormData:fileData name:@"file"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上傳成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上傳失敗");
}];
上傳方法2
直接用URL獲取要上傳的文件
// 1.創建一個管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封裝參數(這個字典只能放非文件參數)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"age"] = @20;
params[@"pwd"] = @"456";
params[@"height"] = @1.55;
// 2.發送一個請求
NSString *url = @"http://localhost:8080/TFServer/upload";
[mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在發送請求之前會自動調用這個block
// 需要在這個block中添加文件參數到formData中
/**
FileURL : 需要上傳的文件的URL路徑
name : 服務器那邊接收文件用的參數名
fileName : (告訴服務器)所上傳文件的文件名
mimeType : 所上傳文件的文件類型
*/
NSURL *url = [[NSBundle mainBundle] URLForResource:@"1" withExtension:@"jpg"];
NSLog(@"-----%@", url);
[formData appendPartWithFileURL:url name:@"file" fileName:@"test.jpg" mimeType:@"image/jpeg" error:nil];
/**
FileData : 需要上傳的文件的具體數據
name : 服務器那邊接收文件用的參數名
fileName : (告訴服務器)所上傳文件的文件名
mimeType : 所上傳文件的文件類型
*/
// UIImage *image = [UIImage imageNamed:@"minion_01"];
// NSData *fileData = UIImagePNGRepresentation(image);
// [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上傳成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上傳失敗");
}];