转载自:http://blog.csdn.net/codywangziham01/article/details/38088017
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 = @"语言-语法预览.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:@"" 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:@"" parameters:nil constructingBodyWithBlock:^(id 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:@"" parameters:nil constructingBodyWithBlock:^(id 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:@"" 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:@"" 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:@"" 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:@"" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
-
NSLog(@"%@", responseObject);
-
-
-
NSLog(@"%@", [NSThread currentThread]);
-
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
-
NSLog(@"%@", error);
-
}];
-
}
-
-
@end
阅读(1456) | 评论(0) | 转发(0) |