AFN Introduction
- AFN is a third-party framework for operating networks
- Nsurlconnection iOS2.0 after the show
- Nsurlsession iOS7.0 after the show
- Default support for serializing and deserializing json,xml data requires self-parsing
- is a good set of error handling mechanisms
- Encapsulates the reachability
- Support for HTTPS (iOS9.0 default is HTTPS network protocol, but can modify properties in Into.pilst for all protocols)
Get DemoURL is a string, if there are characters and spaces need URL encoding to get the data directly deserialized (if not the JSON data obtained in error) block execution is on the main thread code://Request Operations Manager--Nsurlconnection Package
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanagermanager];
[Manager get:@ "http://127.0.0.1/index1.html" parameters:nilsuccess:^ (afhttprequestoperation *operation, ID Responseobject) {
NSLog (@ "%@%@", operation,responseobject);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "Error%@", error); }];get Demo with parameters:AFN will automatically convert the dictionary to the URL after the parameter plus? username=admin&password=123//Request Operations Manager-Package for Nsurlconnection
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanagermanager];
Parameters for Get Requests
Nsdictionary *params = @{@ "username": @ "admin" @ "password": @ "123"};
[Manager get:@ "http://127.0.0.1/php/login.php" Parameters:params success:^ (afhttprequestoperation *operation, ID Responseobject) {
NSLog (@ "%@", responseobject);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "Error%@", error); }];Post DemoRequest Operations Manager--Encapsulation of nsurlconnection
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanagermanager];
Parameters for Get Requests
Nsdictionary *params = @{@ "username": @ "admin" @ "password": @ "123"};
[Manager post:@ "http://127.0.0.1/php/login.php" Parameters:params success:^ (afhttprequestoperation *operation, ID Responseobject) {
NSLog (@ "%@", responseobject);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "Error%@", error); }];serialization mechanism of iOSMethods of Afhttprequestoperationmanager Initialization
? self.requestserializer = [Afhttprequestserializer serializer];
Serialization of request data
? self.responseserializer = [Afjsonresponseserializer serializer];
Serialization of returned data
<afurlrequestserialization> data sent to the server
? Afhttprequestserializer binary (default)
? Afjsonrequestserializer JSON
? Afpropertylistrequestserializer plist
<afurlresponseserialization> data received from the server
? Afhttpresponseserializer binary
? Afjsonresponseserializer JSON (default)
? Afxmlparserresponseserializer Xmlparser (sax parsing XML)
? Afxmldocumentresponseserializer DOM Parsing xml(Mac OS X)
? Afpropertylistresponseserializer plist
? Afimageresponseserializer image? Afcompoundresponseserializer Combinationtwo ways to parse xml:
- Parsing XML in sax mode
- Parsing XML in DOM mode
Sax is to pass the data in the XML file 1.1 points to the DOM is to load the data in the XML file one time, requiring a large memory consumptionAFN class StructureCommon classes in 1.AFN
A.nsurlconnection
I.afurlconnectionoperation
Ii. Afhttprequestoperation
Iii. Afhttprequestoperationmanager
B.nsurlsession
I.afurlsessionmanager
Ii. Afhttpsessionmanager
C.reachability
D.serialization
I.afurlrequestserialization serialization processor for request data
1.AFHTTPRequestSerializer
2.AFJSONRequestSerializer
3.AFPropertyListRequestSerializer
Ii. Afurlresponseserialization serialization processor for response data
1.AFHTTPResponseSerializer
2.AFJSONResponseSerializer
3.AFXMLParserResponseSerializer
4.AFXMLDocumentResponseSerializer
5.AFPropertyListResponseSerializer
6.AFImageResponseSerializer 7.AFCompoundResponseSerializercommon content in ManagerAfhttprequestoperationmanager
A. Properties
I.baseurl
Ii.requestserializer
Iii.responseserializer
Iv.operationqueue
V.reachabilitymanager
B. Methods
I.manager
Ii. Httprequestoperationwithrequest
Iii. GET
iv. HEAD
V.post
Vi. POST Constructingbodywithblock FormData
vii. Putviii.delete
Analysis and use of iOS network programming afnetwording framework