库AFNetworking的使用

库AFNetworking的使用

库AFNetworking的使用

1.GET请求(html,json,xml)

#pragma mark -GET请求
-(void)testGetRequset{ NSString *urlString = @"http://www.baidu.com";
urlString = @"http://m.weather.com.cn/data/101010100.html"; //定义AFNetworking管理
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//注意: 默认认为数据是JSON,content-type也是JSON, responseObject是解析的字典和数组, 不是产生错误Code=-1016
//解决: 设置解析器为HTTP形式, 下载回来是NSData
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //responseObject重要参数,包含下载数据
//NSLog(@"o = %@",responseObject);
NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error = %@",error);
}];
}

2.POST请求

#pragma mark - POST请求
-(void)testPostRequest{ //POST接口:http://quiet.local/testdir/login.php
//参数1: @"user" : @"quiet"
//参数2: @"password" : @"123"
NSString *urlString = @"http://quiet.local/testdir/login.php"; AFHTTPRequestOperationManager *mamager = [AFHTTPRequestOperationManager manager];
mamager.responseSerializer = [AFHTTPResponseSerializer serializer]; //参数1: 传入地址
//参数2: 传入URL请求的参数, 格式传入字典
[mamager POST:urlString parameters:@{@"user":@"quiet",@"password":@"123"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error = %@",error);
}];
}

3.上传文件(上传图片)

#pragma mark - 上传文件(上传图片)
-(void)testUploadFile{
//POST上传接口
NSString *urlString = @"http://quiet.local/uploadtest/upload.php";
//参数名: image : 参数值是图片
AFHTTPRequestOperationManager *mamager = [AFHTTPRequestOperationManager manager];
mamager.responseSerializer = [AFHTTPResponseSerializer serializer];
[mamager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //实现: 上传的数据附加到请求体中
//mimeType 多用途互联网邮件扩展类型, 每种文件都有一个类型
NSString *path = [[NSBundle mainBundle] pathForResource:@"2.jpg" ofType:nil];
NSLog(@"path = %@",path);
[formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"image" fileName:@"quiet1.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);
}];
}

4.下载文件(下载图片)

#pragma mark -下载文件
-(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(@"path = %@",path);
return [NSURL fileURLWithPath:path]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"下载完成");
}];
//启动任务
[task resume];
}

5.监控网络状态

-(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]; }
05-11 13:32