#import "ViewController.h"
#import "AFNetworking.h"
#import "UIKit+AFNetworking.h"
@interface ViewController ()
@property(nonatomic, strong)UIProgressView *progressView;
@end
@implementation ZKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
self.progressView.backgroundColor = [UIColor redColor];
self.progressView.frame = CGRectMake(10, 100, 300, 0);
[self.view addSubview:self.progressView];
[self testAsycnImage];
}
-(void)testNetworkStatus{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"%@",@[@"不可达",@"GPRS",@"WiFi"][status]);
}];
[manager.reachabilityManager startMonitoring];
}
-(void)testAsycnImage{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
[self.view addSubview:imageView];
NSString *path = @"http://10.0.157.3/iOS_PHP/upload/image1";
[imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
imageView.frame = CGRectMake(10, 10, image.size.width, image.size.height);
imageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
}];
}
-(void)testDownloadData{
NSString *path = @"http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip";
AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSString *path = [NSString stringWithFormat:@"%@/documents/downloadfile.zip",NSHomeDirectory()];
NSLog(@"%@",path);
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error == nil) {
NSLog(@"下载成功");
}else{
NSLog(@"下载失败:error:%@",error);
}
}];
[downloadTask resume];
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
float changeValue = [[object valueForKey:@"fractionCompleted"] floatValue];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.progressView.progress = changeValue;
}];
}
-(void)testUploadImage{
NSString *path = @"http://127.0.0.1/iOS_PHP/upload.php";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:path parameters:@{@"user":@"zhangsan",@"password":@"123456"} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"menu_bg_01-hd.jpg" ofType:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:imagePath] name:@"file" fileName:@"image1.png" mimeType:@"image/jpg" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
-(void)testPost{
NSString *path = @"http://127.0.0.1/iOS_PHP/login";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:path parameters:@{@"user":@"zhangsan",@"password":@"123456"} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
-(void)testJSON{
NSString *path = @"http://www.weather.com.cn/data/cityinfo/101010100.html";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
-(void)testGetRequest{
NSString *path= @"http://www.baidu.com";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"string:%@",string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end