衆所周知,蘋果搞的一套框架NSContention發送請求與接收請求的方式十分繁瑣。操做起來很不方便。不只要作區分各類請求設置各類不一樣的參數,並且還要常常在多線程裏操做,同時還要對請求與返回的數據作各類序列化的操做,同時還要考慮請求數據的安全等一堆問題。php
AFNetworking的出現:MAC/iOS設計的一套網絡框架.(爲了簡化網絡操做)git
2、AFNetworking的使用github
關於修改AFN源碼:一般序列化時作對text/plan等的支持時,能夠一勞永逸的修改源代碼,在acceptableContentTypes中修改便可。json
AFN進行GET、POST登陸:安全
#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等)網絡
#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"]; //這個能夠直接往框架裏面修改. | 項目中搜索xuneng, 找到定位點. //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)多線程
#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); }]; }