GET请求

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 

 [manager GET:URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {  

 }
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"这里打印请求成功要做的事"); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@",error); //这里打印错误信息 }];

POST 请求

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

 NSMutableDictionary *parameters = @{@"":@"",@"":@""};

 [manager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {

 } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

 }];

下载

 //1.创建管理者对象
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration]; //2.确定请求的URL地址
NSURL *url = [NSURL URLWithString:@""]; //3.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url]; //下载任务
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//打印下下载进度
NSLog(@"%lf",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//下载地址
NSLog(@"默认下载地址:%@",targetPath); //设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"%@", path);
return [NSURL fileURLWithPath:path]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { //下载完成调用的方法
NSLog(@"下载完成:");
NSLog(@"%@--%@",response,filePath); }]; //开始启动任务
[task resume];

上传

 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];

监测当前网络状态(网络监听)

 {
// 进行网络监测判断的BOOL值
BOOL isOpen;
}
 if (!isOpen) {

     // 打开网络监测的方法
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
isOpen = YES; } else { // 关闭网络监测
[[AFNetworkReachabilityManager sharedManager] stopMonitoring];
isOpen = NO;
} // 接下来会判断是WiFi状态,还是3g或4g状态,还有网络不可用状态
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知状态");
break; case AFNetworkReachabilityStatusNotReachable:
NSLog(@"未连接状态");
break; case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"手机流量网络");
break; case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi状态");
break; default:
break;
}
}];
 

AFNetworking简单用法的更多相关文章

  1. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  2. jquery.validate.js 表单验证简单用法

    引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...

  3. NSCharacterSet 简单用法

    NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...

  4. [转]Valgrind简单用法

    [转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...

  5. Oracle的substr函数简单用法

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  6. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

  7. TransactionScope简单用法

    记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...

  8. WPF之Treeview控件简单用法

    TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...

  9. listActivity和ExpandableListActivity的简单用法

    http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...

随机推荐

  1. Android Studio快捷键每日一练(6)

    原文地址:http://www.developerphil.com/android-studio-tips-of-the-day-roundup-6/ 51.重构代码 苹果:Ctrl+T    Win ...

  2. LeetCode - Triangle

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  3. [Asp.net 5] Configuration-新一代的配置文件(神奇的Binder)

    关于配置文件的目录:[Asp.net 5] Configuration-新一代的配置文件 之前看过MVC4.0的源码,里面就有Binder.作用是将前台页面传递过来的键值对/字典表绑定到特定的对象.此 ...

  4. 【译】About the Java Technology

    About the Java Technology Java technology is both a programming language and a platform. The Java Pr ...

  5. C# 之httpwatch 缩减HttpWatch成可以进行二次开发的代码

    写在前面 本文由来 特别鸣谢 支持开源 1. 写在前面 也是由于项目需要,之前对抓包,有两个方向的理解 1.使用代理抓包,自己写一个中转服务器,就可用拿到,发送和服务器返回的任何数据了.(因为操作的时 ...

  6. SSH实例(4)

    Clas.hbm.xml文件如下: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibe ...

  7. Retrieving Out Params From a Stored Procedure With Python

    http://www.rodneyoliver.com/blog/2013/08/08/retrieving-out-params-from-a-stored-procedure-with-pytho ...

  8. how-to-redirect-cin-and-cout-to-files

    #include <iostream> #include <fstream> #include <string> void f() { std::string li ...

  9. Maven依赖Scope标签用法

    在一个maven项目中,如果存在编译需要而发布不需要的jar包,可以用scope标签,值设为provided.如下: <dependency>            <groupId ...

  10. 泛函编程(32)-泛函IO:IO Monad

    由于泛函编程非常重视函数组合(function composition),任何带有副作用(side effect)的函数都无法实现函数组合,所以必须把包含外界影响(effectful)副作用不纯代码( ...