国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

AFN 網絡操作與AFNetworking

2019-11-09 17:54:51
字體:
來源:轉載
供稿:網友
1、AFN特性 :*登錄傳參數時,傳遞字典即可.(鍵名為參數名,鍵值為參數值).*自動到子線程中執行,執行完后返回主線程.*返回的結果自動序列化為NSDictionary.2、使用AFN注意 :*AFHTTPRequestOperationManager封裝了通過HTTP協議與Web應用程序進行通訊的常用方法.(這個實例化的時候不是單例, 因為沒有shared字)*包括創建請求/響應序列化/網絡監控/數據安全.*方法等都是以AF開頭的.

3、AFN能做的 (網絡中的都涵蓋了):*GET/POST/PUT/DELETE/HEAD請求.*JSON數據解析/Plist數據解析.(不支持xml數據解析)*POSTJSON.*上傳/下載.

4、使用步驟 : (可參考說明文檔)1.首先需要實例化一個請求管理器AFHTTPRequestOperationManager.2.設置請求的數據格式:默認是二進制.(不是可改)*AFHTTPRequestSerializer(二進制)*AFJSONRequestSerializer(JSON)*AFPropertyListRequestSerializer(Plist)3.設置響應的數據格式:默認是JSON.(不是可改)*AFHTTPResponseSerializer(二進制)*AFJSONResponseSerializer(JSON)*AFPropertyListResponseSerializer(Plist)*AFXMLParserResponseSerializer(XML)*AFImageResponseSerializer(Image)*AFCompoundResponseSerializer(組合的)4.如果響應者的MIMEType不正確,就要修改acceptableContentTypes.5.調用方法,發送響應的請求(GET/POST...).

關于修改AFN源碼:通常序列化時做對text/plan等的支持時,可以一勞永逸的修改源代碼,在acceptableContentTypes中修改即可。

AFN進行GET、POST登錄:

[objc] view plaincopy#pragma mark - get/post登錄  - (void)getLogin {      //1.管理器      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];        //2.設置登錄參數      NSDictionary *dict = @{ @"username":@"xn", @"passWord":@"123" };        //3.請求      [manager GET:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responSEObject) {          NSLog(@"GET --> %@, %@", responseObject, [NSThread currentThread]); //自動返回主線程      } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {          NSLog(@"%@", error);      }];  }    /**  *  和上面的GET用法完全一樣, 只有一個POST參數不一樣  */  - (void)postLogin {      //1.管理器      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];        //2.設置登錄參數      NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };        //3.請求      [manager POST:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {          NSLog(@"POST --> %@, %@", responseObject, [NSThread currentThread]); //自動返回主線程      } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {          NSLog(@"%@", error);      }];  }  AFN進行網絡數據解析,獲取Plist,JSON,XML(AFN不支持自動解析XML,有專門的框架去做,如SAX,PULL,KissXML等)

[objc] view plaincopy#pragma mark - get 數據解析  - (void)getJSON {      //1.請求管理器      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];        //2.發起請求      [manager GET:@"http://localhost/videos.json" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {          NSLog(@"%@", responseObject);      } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {          NSLog(@"%@", error);      }];  }    /**  *  不支持XML數據解析  */  - (void)getXML {      //1.管理器      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];        //2.設置返回數據類型      manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; //先實例化一下        //3.發起請求      [manager GET:@"http://localhost/videos.xml" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {          NSLog(@"%@", responseObject);      } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {          NSLog(@"%@", error);      }];  }    - (void)getPlist {      //1.管理器      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];        //2.設置response類型      manager.responseSerializer = [AFPropertyListResponseSerializer serializer]; //是Response, 別寫成request了. 修改為plist類型.      manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; //這個可以直接往框架里面修改.        //3.請求      [manager GET:@"http://localhost/videos.plist" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {          NSLog(@"%@", responseObject);      } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {          NSLog(@"%@", error);      }];  }  用AFN來POST JSON數據,上傳、下載等。(上傳、下載主頁說明上有https://github.com/AFNetworking/AFNetworking)

[objc] view plaincopy#pragma mark - post json數據與上傳文件等  - (void)postJSON {      //1.管理器      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];        //2.設定類型. (這里要設置request-response的類型)      manager.requestSerializer = [AFJSONRequestSerializer serializer];      manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //這個決定了下面responseObject返回的類型  //    manager.responseSerializer = [AFJSONResponseSerializer serializer];  //  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];            //2.設置登錄參數      NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };        //3.發送請求      [manager POST:@"http://localhost/postjson.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {  //      NSLog(@"postjson--> %@", responseObject);  //這樣顯示JSON的話需要設置text/plain          NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];          NSLog(@"%@",result);      } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {          NSLog(@"%@", error);      }];  } 轉載自:http://blog.csdn.net/xn4545945  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潍坊市| 大石桥市| 军事| 大悟县| 桐庐县| 闽侯县| 宝兴县| 高密市| 黄浦区| 普安县| 左云县| 五峰| 汤阴县| 辽阳县| 竹溪县| 福泉市| 镇沅| 精河县| 朝阳县| 卢龙县| 广西| 阜宁县| 铜陵市| 枣阳市| 修水县| 阜康市| 平邑县| 桐乡市| 资阳市| 新闻| 斗六市| 孟津县| 铜川市| 稻城县| 盘山县| 迭部县| 呼伦贝尔市| 文昌市| 昌吉市| 社旗县| 星子县|