///////////////////////////以前用法/////////////////////////////////////////////////////////////

//使用AFHTTPRequestOperation 创建HTTP请求 --- GET请求

//1,url

NSString *URLTmp = @"http://api.budejie.com/api/api_open.php?a=list&appname=baisibudejie&asid=F20FB3C5-A3A6-4ACA-9B22-44FB308FCB23&c=data&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=1&mac=02%3A00%3A00%3A00%3A00%3A00&market=&openudid=6bcaf1e795d8e4cf82ce78caa1fc33b9a7c671d5&page=0&per=20&type=10&udid=&ver=2.9.3";

URLTmp = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码成UTF-8  否则可能会出现错误

//2,请求对象

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: URLTmp]];

//3,创建HTTP请求

AFHTTPRequestOperation *afOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//4,

afOperation.responseSerializer = [AFJSONResponseSerializer serializer];

//5,开始请求

[afOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

//将字符串 编码后 转成 NSData

NSData *resData = [[NSData alloc] initWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding]];

//系统自带JSON解析

NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];

NSLog(@"%@",resultDic);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Failure: %@", error);

}];

//6,

[[NSOperationQueue mainQueue] addOperation:afOperation];

//------批量处理

//    NSMutableArray *mutableOperations = [NSMutableArray array];

//    for (NSURL *fileURL in filesToUpload) {

//        NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

//            [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];

//        }];

//

//        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

//

//        [mutableOperations addObject:operation];

//    }

//

//    NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

//        NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);

//    } completionBlock:^(NSArray *operations) {

//        NSLog(@"All operations in batch complete");

//    }];

//    [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

//

///////////////////////////新用法/////////////////////////////////////////////////////////////

//提交GET ,

//1,创建AFHTTPRequestOperationManager HTTP请求操作管理对象

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//2,发送GET请求(默认解析JSON,plist. )

[manager GET:URLTmp

parameters:nil

success:^(AFHTTPRequestOperation *operation, id responseObject) {

// NSLog(@"JSON: %@", responseObject);

}

failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Error: %@", error);

}];

// POST 单个请求

//1,创建AFHTTPRequestOperationManager HTTP请求操作管理对象

AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];

//2,发送POST请求(默认解析JSON,plist. )

NSDictionary *parameters = @{ };//空参数

[manager1 GET:URLTmp

parameters:parameters  //参数

success:^(AFHTTPRequestOperation *operation, id responseObject) {

// NSLog(@" POST JSON: %@", responseObject);

}

failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Error: %@", error);

}];

//POST 多个请求

AFHTTPRequestOperationManager *manager2 = [AFHTTPRequestOperationManager manager];

NSDictionary *parameters2 = @{};

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];

[manager2 POST:@"http://example.com/resources.json"

parameters:parameters2

constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFileURL:filePath name:@"image" error:nil];

}

success:^(AFHTTPRequestOperation *operation, id responseObject) {

//  NSLog(@"Success: %@", responseObject);

}

failure:^(AFHTTPRequestOperation *operation, NSError *error) {

//NSLog(@"Error: %@", error);

}];

//

////////////////////////////////////////////////////////////////////////////////////////

//创建一个下载文件的任务

//AFURLSessionManager创建并完善了一个NSURLSession的对象基于遵从NSURLSessionDelegate与NSURLSessionDataDelegate协议 NSURLSessionConfigration对象

//1,1 创建配置对象  (上传 下载 使用 ,  配置超时值,缓存策略,连接要求,和其他类型的信息,)

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

//1.2 通过配置对象,创建AFURLSessionManager 管理对象

AFURLSessionManager *managerDown = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

//2,请求对象

NSURL *URL = [NSURL URLWithString:@"http://img4.mypsd.com.cn/20110707/Mypsd_59710_201107071333160001B.jpg"];

NSURLRequest *requestDown = [NSURLRequest requestWithURL:URL];

//3,创建下载任务

NSURLSessionDownloadTask *downloadTask = [managerDown downloadTaskWithRequest:requestDown

progress:nil

destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

// 3,1 返回 一个下载到的路径

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; //文件路径

}

completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

//此处已经回到主线程 (处理UI)

// NSLog(@"File downloaded to: %@", filePath);

}];

//4, 开始

[downloadTask resume];

////////////////////////////////////////////////////////////////////////////////////////

//创建一个上传文件的任务

//AFURLSessionManager创建并完善了一个NSURLSession的对象基于遵从NSURLSessionDelegate与NSURLSessionDataDelegate协议 NSURLSessionConfigration对象

//1,1 创建配置对象  (上传 下载 使用 ,  配置超时值,缓存策略,连接要求,和其他类型的信息,)

NSURLSessionConfiguration *configuration1 = [NSURLSessionConfiguration defaultSessionConfiguration];

//1.2 通过配置对象,创建AFURLSessionManager 管理对象

AFURLSessionManager *managerUp = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration1];

//2,请求对象

NSURL *upURL = [NSURL URLWithString:@"http://example.com/upload"];

NSURLRequest *requestUp = [NSURLRequest requestWithURL:upURL];

//3,创建上传任务

NSURLSessionUploadTask *uploadTask = [managerUp uploadTaskWithRequest:requestUp

fromFile:[NSURL fileURLWithPath:@"file://path/to/image.png"]//上传文件的URL

progress:nil

completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

if (error) {

//  NSLog(@"Error: %@", error);

} else {

// NSLog(@"Success: %@ %@", response, responseObject);

}

}];

//4,开始

[uploadTask resume];

AFNetworking 使用 核心代码的更多相关文章

  1. Android发送短信核心代码

    核心代码:(1)SmsManager manager = SmsManager.getDefault(); //获得默认的消息管理器(2)ArrayList<String> list = ...

  2. [html5+java]文件异步读取及上传核心代码

    html5+java 文件异步读取及上传关键代码段 功能: 1.多文件文件拖拽上传,file input 多文件选择 2.html5 File Api 异步FormData,blob上传,图片显示 3 ...

  3. 【五子棋AI循序渐进】关于VCT,VCF的思考和核心代码

    前面几篇发布了一些有关五子棋的基本算法,其中有一些BUG也有很多值得再次思考的问题,在框架和效果上基本达到了一个简单的AI的水平,当然,我也是初学并没有掌握太多的高级技术.对于这个程序现在还在优化当中 ...

  4. .NET核心代码保护策略-隐藏核心程序集

    经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德高人的.想想还是算了,那样估计会引来新一波 ...

  5. 处理部分WordPress核心代码或功能,让你的网站更快

    处理部分WordPress核心代码或功能,让你的网站更快 http://www.wpdaxue.com/speed-up-wordpress.html

  6. OC 冒泡排序 -- 核心代码

    //冒泡 核心代码 for (int i = 0; i < array.count - 1; i++) { int a = [array[i] intValue]; for (int j = i ...

  7. .NET核心代码保护策略

    .NET核心代码保护策略-隐藏核心程序集 经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德 ...

  8. js小功能合集:计算指定时间距今多久、评论树核心代码、字符串替换和去除。

    1.计算指定时间距今多久 var date1=new Date('2017/02/08 17:00'); //开始时间 var date2=new Date(); //当前时间 var date3=d ...

  9. 学习Redux之分析Redux核心代码分析

    1. React,Flux简单介绍 学习React我们知道,React自带View和Controller库,因此,实现过程中不需要其他任何库,也可以独立开发应用.但是,随着应用程序规模的增大,其需要控 ...

随机推荐

  1. contentInset,contentsize和contentOffset区别

    contentInset,contentsize和contentOffset区别   今天看别人用到了contentInset,觉得很迷糊,于是gogle了一下,搜到了一篇分析的很好的文章,转在这里, ...

  2. PHP安装OPENSSL扩展模块

    新项目上线时,PHP开发同事反映邮件功能不能正常使用. 原来是用465的SMTP加密端口,不是25端口.那要为当前的PHP安装OPENSSL扩展啦. 还好,网上有很多,弄一个过来就搞定. http:/ ...

  3. MCS-51单片机实用子程序库

    目前已有若干版本的子程序库公开发表,它们各有特色.本程序库中的开平方算法为快速逼近算法,它能达到牛顿迭代法同样的精度,而速度加快二十倍左右,超过双字节定点除法的速度. 本子程序库对<单片机应用程 ...

  4. How can I let the compiled script depend on something dynamic

    Compile your script with /DNAME=value or /X"nsis command" passed on to makensis.exe as com ...

  5. Linux企业级项目实践之网络爬虫(20)——扩展成为规则插件模式

    为了方便我们爬虫功能的扩展,最好使用插件机制.使用插件技术能够在分析.设计.开发.项目计划.协作生产和产品扩展等很多方面带来好处:(1)结构清晰.易于理解.由于借鉴了硬件总线的结构,而且各个插件之间是 ...

  6. c语言中break continue goto return和exit的区别 联系(筛选奇数和goto求和)

    break 一般你是用于循环和switch语句中,执行break,退出循环,如果是多层循环,那么退出的当前的循环. 如果循环结构中有switch语句,而switch语句中有break 那么不会直接退出 ...

  7. 第27讲 UI组件之 ScrollView与底部动态添加数据

    第27讲 UI组件之 ScrollView与底部动态添加数据 1. ScrollView(滚动视图) ScrollView(滚动视图)是实现滚动的一个控件,只需要将需要滚动的控件添加到ScrollVi ...

  8. uiautomatorviewer 可以查看到android中的web 元素信息

    以知乎社区账号登录使用微博账号为例,使用uiautomatorviewer 可以定位到登录框.密码框,需要结合appium的inspector 1.genymotion 模拟器开启,模拟器安卓系统为4 ...

  9. POJ2151Check the difficulty of problems 概率DP

    概率DP,还是有点恶心的哈,这道题目真是绕,问你T个队伍.m个题目.每一个队伍做出哪道题的概率都给了.冠军队伍至少也解除n道题目,全部队伍都要出题,问你概率为多少? 一開始感觉是个二维的,然后推啊推啊 ...

  10. HOG(方向梯度直方图)

    结合这周看的论文,我对这周研究的Histogram of oriented gradients(HOG)谈谈自己的理解: HOG descriptors 是应用在计算机视觉和图像处理领域,用于目标检測 ...