一群程序猿的秘密基地iOS开发AFNetworking

AFNetworking 详解 以及 通过 POST 方式上传数

2016-08-10  本文已影响13406人  hx永恒之恋

POST 发送数据有两种形式:
1、发送纯文本的内容
2、发送的 body 部分带有文件(图片,音频或者其他二进制数据)

对应的 Content-Type 有两种:
1、application/x-www-form-urlencoded
2、multipart/form-data

传统的使用 POST 的方式发送数据用于上传文件,AFNetworking 中提供了直接的接口:

 [self.manager POST:post_url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          // 直接以 key value 的形式向 formData 中追加二进制数据
          [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] 
          name:@"key1"];
          [formData appendPartWithFileData:imgData name:@"imagefile" fileName:@"img.jpg" mimeType:@"image/jpeg"];
 }success:^(AFHTTPRequestOperation *operation, id responseObject) {
          // 成功后的处理
 }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          // 失败后的处理
 }];

使用 POST 方式发送纯文本内容:

 - (NSMutableURLRequest *)postRequestWithURL:(NSString *)url content:(NSString *)text
{
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
      [request setHTTPMethod:@"POST"];
      [request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Contsetent-Type"];
      [request setHTTPBody:1];

  return request;
}

     NSOperation *operation =[self.manager HTTPRequestOperationWithRequest:request      
       success:^(AFHTTPRequestOperation *operation, id responseObject) {
     // 成功后的处理
     }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     // 失败后的处理
     }];

     [self.manager.operationQueue addOperation:operation];
     其中 self.manager 为 AFHTTPRequestOperationManager 实例。
     _manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];

     // 对于网站成功返回 JSON 格式的数据但是却在 failure 回调中显示的,
     // 是因为服务器返回数据的网页中 content type 没有设置为 text/json
     // 对于我们公司的服务器返回的 content type 为 text/html 所以我设置为如下这样,
     // 对于不同的情况可以根据自己的情况设置合适的接受的 content type 的类型
       _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

AFN
一、什么是AFN
全称是AFNetworking,是对NSURLConnection的一层封装
虽然运行效率没有ASI高,但是使用比ASI简单
在iOS开发中,使用比较广泛

AFN的github地址
https://github.com/pokeb/AFNetworking/AFNetworking

二、AFN结构
NSURLConnection
AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager(封装了常用的 HTTP 方法)
属性
baseURL :AFN建议开发者针对 AFHTTPRequestOperationManager 自定义个一个单例子类,设置 baseURL, 所有的网络访问,都只使用相对路径即可
requestSerializer :请求数据格式/默认是二进制的 HTTP
responseSerializer :响应的数据格式/默认是 JSON 格式
operationQueue
reachabilityManager :网络连接管理器

方法
manager :方便创建管理器的类方法
HTTPRequestOperationWithRequest :在访问服务器时,如果要告诉服务器一些附加信息,都需要在 Request 中设置
GET
POST

NSURLSession
AFURLSessionManager
AFHTTPSessionManager(封装了常用的 HTTP 方法)
GET
POST
UIKit + AFNetworking 分类
NSProgress :利用KVO

半自动的序列化&反序列化的功能
AFURLRequestSerialization :请求的数据格式/默认是二进制的
AFURLResponseSerialization :响应的数据格式/默认是JSON格式

附加功能
安全策略
HTTPS
AFSecurityPolicy

网络检测
对苹果的网络连接检测做了一个封装
AFNetworkReachabilityManager

三、AFN基本使用
1.AFHTTPSessionManager
是AFN中最重要的对象之一
封装了HTTP请求的常见处理
GET\POST请求
解析服务器的响应数据

创建AFHTTPSessionManager
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

AFHTTPSessionManager的GET\POST请求

![ ![ ![ ![ ![ ![ ![ ![ ![ ![ ![ ![Uploading 2323089-ffd30fab43c4efd0_116702.jpg . . .] ](http://upload-images.jianshu.io/upload_images/2323089-d4f71795d382389c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-7ec0c11234613a24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-da952913a1bc9209.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-fcf12d1f0df27e18.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-ee8da9f3b9fcb315.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-a78b8ffce05427ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-319693e390bfadb6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-dda4cc06d9ca8564.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-5085a6983baed619.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-2214a869f38f09e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/2323089-0cc1eadd7ee293cb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上一篇下一篇

猜你喜欢

热点阅读