标签:
AFNetworking是一个轻量级的iOS网络通信类库。它建立在NSURLConnection和NSOperation等类库的基础上。它支持HTTP请求和基于REST的网络服务(包括GET、POST、 PUT、DELETE等)。支持ARC。
-
- #import "MJViewController.h"
- #import "AFNetworking.h"
-
- @interface MJViewController ()
-
- @end
-
- @implementation MJViewController
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- [self reach];
- }
-
- #pragma mark - 演练
- #pragma mark - 检测网络连接
- - (void)reach
- {
-
-
- [[AFNetworkReachabilityManager sharedManager] startMonitoring];
-
-
- [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
- NSLog(@"%d", status);
- }];
- }
-
- #pragma mark - Session 下载
- - (void)sessionDownload
- {
- NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];
-
- NSString *urlString = @"http://localhost/itcast/videos/01.C语言-语法预览.mp4";
- urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-
- NSURL *url = [NSURL URLWithString:urlString];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
-
-
- NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
- NSString *path = [cacheDir stringByAppendingPathComponent:response.suggestedFilename];
-
-
- NSURL *fileURL1 = [NSURL URLWithString:path];
- NSURL *fileURL = [NSURL fileURLWithPath:path];
-
- NSLog(@"== %@ |||| %@", fileURL1, fileURL);
-
- return fileURL;
- } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
- NSLog(@"%@ %@", filePath, error);
- }];
-
- [task resume];
- }
-
- #pragma mark - POST JSON
- - (void)postJSON
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
- NSDictionary *dict = @{@"name": @"zhangsan"};
- NSDictionary *dict1 = @{@"name": @"wangwu"};
- NSArray *array = @[dict, dict1];
-
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
-
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
- [manager POST:@"http://localhost/postjson.php" parameters:array success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
-
- NSLog(@"%@", result);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
-
- }];
-
- }
-
- #pragma mark - 随机文件名上传
- - (void)postUpload1
- {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
- [manager POST:@"http://localhost/demo/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
-
- NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"头像1.png" withExtension:nil];
-
-
-
-
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
-
- formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
- NSString *fileName = [formatter stringFromDate:[NSDate date]];
-
- [formData appendPartWithFileURL:fileURL name:@"uploadFile" fileName:fileName mimeType:@"image/png" error:NULL];
-
- } success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"OK");
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"error");
- }];
- }
- #pragma mark - POST上传
- - (void)postUpload
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
-
-
-
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
-
- [manager POST:@"http://localhost/demo/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
-
-
- NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"头像1.png" withExtension:nil];
-
- [formData appendPartWithFileURL:fileURL name:@"uploadFile" error:NULL];
- } success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
-
- NSLog(@"完成 %@", result);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"错误 %@", error.localizedDescription);
- }];
- }
-
- #pragma mark - JSON
- - (void)XMLData
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
-
- NSDictionary *dict = @{@"format": @"xml"};
-
-
- [manager GET:@"http://localhost/videos.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
-
-
-
- NSLog(@"%@", responseObject);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- #pragma mark - JSON
- - (void)JSONData
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{@"format": @"json"};
-
-
- [manager GET:@"http://localhost/videos.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
-
-
- NSLog(@"%@", [NSThread currentThread]);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- #pragma mark - POST登录
- - (void)postLogin
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{@"username": @"wangwu", @"password" : @"wang"};
-
-
- [manager POST:@"http://localhost/login.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
-
-
- NSLog(@"%@", [NSThread currentThread]);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- #pragma mark - GET登录
- - (void)getLogin
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{@"username": @"wangwu", @"password" : @"wang"};
-
-
- [manager GET:@"http://localhost/login.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
-
-
- NSLog(@"%@", [NSThread currentThread]);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- @end
图片上传:
1.单张图片上传
//头像转NsData
NSData *userPhotoData = UIImagePNGRepresentation(self.headImagge.image);
NSDictionary *parameters = @{@"token":Apptoken, @"username":personUserName,
@"password":passWord, @"email":userName,
@"birthday":brithDate,@"gender":gendar,
@"language":NSLocalizedString(@"MoodBox_language", nil)};
//提交
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//返回数据类型为NSData
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:registerURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:userPhotoData name:@"userphoto" fileName:@"userphoto.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@", jsonDict);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
2.多张图片上传
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:url parameters:url参数 constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (int i = 0; i<arrayImage.count; i++) {
UIImage *uploadImage = arrayImage[i];
[formData appendPartWithFileData:UIImagePNGRepresentation(uploadImage)name:[NSString stringWithFormat:@"参数%d",i+1] fileName:@"test.jpg"mimeType:@"image/jpg"];
}
} error:nil];
AFHTTPRequestOperation *opration = [[AFHTTPRequestOperationalloc]initWithRequest:request];
opration.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
[opration setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, idresponseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
AFNetworking 使用详解
标签:
原文地址:http://www.cnblogs.com/moyazi/p/4581328.html