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. iOS开发的一些奇巧淫技

    TableView不显示没内容的Cell怎么办? 类似这种,我不想让下面那些空的显示. 很简单. self.tableView.tableFooterView = [[UIView alloc] in ...

  2. C#生成验证码

    生成验证码的类: using System; using System.Collections.Generic; using System.Drawing; using System.Text; na ...

  3. "Hello World!" for the NetBeans IDE

    "Hello World!" for the NetBeans IDE It's time to write your first application! These detai ...

  4. MEF入门之不求甚解,但力求简单能讲明白(二)

    在上一篇文章中,我们已经学到了很基本的MEF概念和使用方法. 但我们导出的是一个object类型的实例,只能用来tostring,没有引用部件类库,也不能用里面的成员方法. 本篇,我们逐渐往简单的文件 ...

  5. Android使用SAX解析XML(3)

    主界面MainActivity.java: package com.hzhi.my_sax; import java.util.ArrayList; import android.os.Bundle; ...

  6. linux下c程序的链接、装载和库(1)

    读完<程序员的自我修养--链接.装载和库>相关章节,想来总结一下,若有错误,请指正,多谢. 1. 什么叫目标文件? 你的工程里有很多xxx.c这样的源文件,这些文件是文本文件,只有人能够认 ...

  7. HTTP协议简解

    1.什么是http协议 http协议: 浏览器客户端 与  服务器端 之间数据传输的规范 2.查看http协议的工具 1)使用火狐的firebug插件(右键->查看元素->网络) 2)使用 ...

  8. Verilog学习笔记基本语法篇(十三)...............Gate门

    Verilog中已有一些建立好的逻辑门和开关的模型.在所涉及的模块中,可通过实例引用这些门与开关模型,从而对模块进行结构化的描述. 逻辑门: and (output,input,...) nand ( ...

  9. node.js图片上传

    1.node-formidable 对文件上传提供帮助的组件 2.app.js var formidable = require('formidable'); var http = require( ...

  10. c# datagridview禁止自动生成额外列

    在某些时候,处于重用pojo的考虑,我们希望在不同的datagridview之间进行复用,这就涉及到pojo中的字段会比有些datagridview所需要的字段多,默认情况下,.net对于pojo中的 ...