// ViewController.m
// 1122
//
// Created by SSP_MACMINI-001 on 15/6/30.
// Copyright (c) 2015年 sunhr. All rights reserved.
//
import "ViewController.h"
import "AFNetworking.h"
import "UIKit+AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//上传文件(上传图片)
-(void)testUploadFile{
//POST上传的接口
NSString *urlString = @"http://quiet.local/uploadtest/pk.php";
//参数名:image : 参数值是图片
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFCompoundResponseSerializer serializer];
[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSString *path = [[NSBundle mainBundle]pathForResource:@"daji.jpg" ofType:nil];
[formData appendPartWithFileURL:[NSURL URLWithString:path] name:@"image" fileName:@"666.jpg" mimeType:@"image/jpeg" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }];
[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//实现:上传的数据附加到请求体中
//fileName:上传后的文件名
//mimeType:需要上网搜索mime,把相应的格式复制过来
NSString *path = [[NSBundle mainBundle]pathForResource:@"daji.jpg" ofType:nil]; [formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"image" fileName:@"666.jpg" mimeType:@"image/jpeg" error:nil]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }];
}
//下载文件
-(void)testDownloadFile
{ NSString *urlString = @"http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip";
//创建会话管理对象(通过默认配置)
AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
//返回文件保存的位置
NSString *path = [NSString stringWithFormat:@"%@/Documents/all.zip",NSHomeDirectory()]; NSLog(@"%@",NSHomeDirectory()); return [NSURL fileURLWithPath:path]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"下载完成"); }];
//启动任务
[task resume];
}
//监控网络状态
-(void)testMonitorNetworkStatus
{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"www.baidu.com"]]; [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSDictionary *dict = @{ @(AFNetworkReachabilityStatusUnknown): @"未知", @(AFNetworkReachabilityStatusNotReachable): @"不可达", @(AFNetworkReachabilityStatusReachableViaWWAN): @"GPRS", @(AFNetworkReachabilityStatusReachableViaWiFi): @"Wifi", }; NSLog(@"状态为 %@",dict[@(status)]); }];
//开启状态监视
[manager.reachabilityManager startMonitoring];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end