AFNetworking 詳解 以及 通過 POST 方式上傳數據

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)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • AFN什么是AFN全稱是AFNetworking,是對NSURLConnection、NSURLSession的一...
    醉葉惜秋閱讀 1,245評論 0 0
  • AFN什么是AFN全稱是AFNetworking,是對NSURLConnection、NSURLSession的一...
    JonesCxy閱讀 1,247評論 0 5
  • 網絡解析:AFNnetworking詳解 一、什么事AFN□在iOS開發中,使用比較廣泛□全稱是AFNetwork...
    Bo_cake閱讀 893評論 0 1
  • 13.服務器圖片改了,url沒有變,需求,服務器的圖片變了,就從服務器加載圖片,如果服務器的沒變,就從本地加載 1...
    AlanGe閱讀 1,207評論 0 1
  • 全稱是AFNetworking,是對NSURLSession的一層封裝 雖然運行效率沒有ASI高,但是使用比ASI...
    翻這個墻閱讀 1,057評論 7 4