? 1, overview
Afnetworking abbreviated as AFN, is one of the third-party structures in the mainstream of iOS development. Apple's set of structure nscontention send begged and accept begged the method is very cumbersome. It is not convenient to operate. Not only need to make a variety of different begged to set a variety of different parameters, but also often in multi-threaded operation, together with the data to be begged and back to do a variety of serialization operations, together with the security of the begged data to consider a pile of questions. And Afnetworking helped us simplify the operation of the network.
2. Several network structures earlier
1. ASI Structure: HTTP terminator. Very good, but there are bugs, has been continuously updated.
2, Mknetworkkit (written by the Indians).
3, AFN has always been updated.
The appearance of afnetworking: a set of network structure designed by Mac/ios. (To simplify network operations)
Address
1. Overview
Afnetworking abbreviated as AFN, is one of the third-party structures in the mainstream of iOS development. Apple's set of structure nscontention send begged and accept begged the way is very cumbersome. It is not convenient to operate. Not only need to make a variety of different begged to set a variety of different parameters, but also often in multi-threaded operation, together with the data to be begged and back to do a variety of serialization operations, together with the security of the begged data to consider a pile of questions. And Afnetworking helped us simplify the operation of the network.
2. Several network structures earlier
1. ASI Structure: HTTP terminator. Very good, but there are bugs, has been continuously updated.
2, Mknetworkkit (written by the Indians).
3, AFN has always been updated.
The appearance of afnetworking: a set of network structure designed by Mac/ios. (To simplify network operations)
Address: http://www.keymob.com/
*afn focus on network data transmission, as well as multi-threading processing in the network.
3, the use of afnetworking
1, AFN Characteristics:
When you log in to pass parameters, you can pass the dictionary. (The key name is the parameter name and the key value is the argument value).
Active to the child thread implementation, after the implementation of the main thread back.
The function of returning is actively serialized into Nsdictionary.
2. Use AFN to be careful:
Afhttprequestoperationmanager encapsulates a common approach to communicating with Web applications through the HTTP protocol. (This instanced time is not a singleton, because there is no shared word)
Contains the establishment of the pleading/care serialization/network monitoring/data security.
Methods, etc. are beginning with AF.
3, AFN can do (the network is covered):
Get/post/put/delete/head begged.
JSON data parsing/plist data parsing. (XML data parsing is not supported)
Postjson.
Upload/download.
4. Application process:
(1) Primary requirements instantiation of a begged manager Afhttprequestoperationmanager.
(2) Set the data format of the begged: acquiescence is binary. (Can not be changed)
Afhttprequestserializer (binary)
Afjsonrequestserializer (JSON)
Afpropertylistrequestserializer (Plist)
(3) Setting the data format for care: acquiescence is JSON. (Can not be changed)
Afhttpresponseserializer (binary)
Afjsonresponseserializer (JSON)
Afpropertylistresponseserializer (Plist)
Afxmlparserresponseserializer (XML)
Afimageresponseserializer (Image)
Afcompoundresponseserializer (combination of)
(4) If the caregiver's mimetype is incorrect, it is necessary to revise the acceptablecontenttypes.
(5) Call the method, send the care of the begged (Get/post ...). Referral website http://www.keymob.com/
For modifying the AFN source code: General Serialization when the support Text/plan and so on, can written off modify the source, in the acceptablecontenttypes can be modified.
AFN for GET, post login:
?
| 123456789101112131415161718192021222324252627282930313233 |
#pragma mark - get/post login- (void) getlogin { //1. Manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager MANAGER];     //2. Setting the login Parameters nsdictionary *dict = @ { @ "username": @ "xn", @ "password": @ "123"  };     //3. Begged [manager get:@ "http://localhost/login.php" parameters:dict success: ^ ( Afhttprequestoperation *operation, id responseobject) { nslog (@ "get --> %@, %@", responseobject, [nsthread CurrentThread]); //active back main thread } failure: ^ (afhttprequestoperation * Operation, nserror *error) { nslog (@ "%@", error); }];} /** * and the Get usage above are identical, only one post parameter is different */- (void) postlogin {  //1. Manager afhttprequestoperationmanager *manager = [ AFHTTPREQUESTOPERATIONMANAGER MANAGER];     //2. Setting Login Parameters nsdictionary *dict = @{ @ "username": @ "xn", @ "password": @ "123" }; //3. Begged [manager post:@ "http://localhost/login.php" parameters:dict success: ^ (Afhttprequestoperation *operation, id responseobject) { nslog (@ "post --> %@, %@", Responseobject, [nsthread currentthread]); //active back main thread } failure: ^ (Afhttprequestoperation *operation, nserror *error) { nslog (@ "%@", error); }];} |
AFN network data analysis, get Plist,json,xml (AFN does not support the active parsing of XML, there is a specific structure to do, such as Sax,pull,kissxml, etc.):
?
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
#pragma mark - get data resolution- (void) getjson { //1. Begged manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager MANAGER];     //2. Recommended [manager get:@ "http://localhost/ Videos.json " parameters:nil success: ^ (afhttprequestoperation *operation, id Responseobject) { nslog (@ "%@", responseobject); } failure: ^ (Afhttprequestoperation *operation, nserror *error) { nslog (@ "%@", error); }];} /** * XML Data resolution */- (void) GETXML {    //1 is not supported. Manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager Manager]; &nBSP;  //2. Set back data type manager.responseserializer = [ afxmlparserresponseserializer serializer]; //first instantiate the      //3. Recommended begged [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. Manager afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];     //2. Setting the response type manager.responseserializer = [ Afpropertylistresponseserializer serializer]; //is response, don't write request . modify to plist type . manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@ "Text/plain"]; / /This can be changed directly into the structure. //3. Begged [manager get:@ "http:// Localhost/videos.plist " parameters:nil success: ^ (afhttprequestoperation *operation, Id responseobject) { nslog (@ "%@", responseObject ); } failure: ^ (afhttprequestoperation *operation, nserror * Error) { nslog (@ "%@", error); }];} |
Use AFN to post JSON data, upload, download, etc.:
?
| 1234567891011121314151617181920212223 |
#pragma mark - post json data with- (void) postjson { //1 such as uploading files. Manager afhttprequestoperationmanager *manager = [ AFHTTPREQUESTOPERATIONMANAGER MANAGER];     //2. Setting Type . ( Here to set the type of Request-response) manager.requestserializer = [ afjsonrequestserializer serializer]; manager.responseserializer = [ afhttpresponseserializer serializer]; //this chooses the type of responseobject back below// manager.responseserializer = [afjsonresponseserializer serializer];// manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@ "Text/plain"];         //2. Setting the login Parameters nsdictionary *dict = @{ @ "username": @ "xn", @ "password": @ "123"  };     //3. Send begged [manager post:@ "http://localhost/postjson.php" parameters:dict success: ^ ( Afhttprequestoperation *operation, id responseobject) {// NSLog (@ "postjson--> %@", responseobject); //so show json. Requirements Settings text/plain nsstring *result = [[nsstring alloc] initwithdata: Responseobject encoding:nsutf8stringencoding]; nslog (@ "%@" , result); } failure: ^ (Afhttprequestoperation *operation, nserror *error) { nslog (@ "%@", error); }];} |
*afn focus on network data transmission, as well as multi-threading processing in the network.
3, the use of afnetworking
1, AFN Characteristics:
When you log in to pass parameters, you can pass the dictionary. (The key name is the parameter name and the key value is the argument value).
Active to the child thread implementation, after the implementation of the main thread back.
The function of returning is actively serialized into Nsdictionary.
2. Use AFN to be careful:
Afhttprequestoperationmanager encapsulates a common approach to communicating with Web applications through the HTTP protocol. (This instanced time is not a singleton, because there is no shared word)
Contains the establishment of the pleading/care serialization/network monitoring/data security.
Methods, etc. are beginning with AF.
3, AFN can do (the network is covered):
Get/post/put/delete/head begged.
JSON data parsing/plist data parsing. (XML data parsing is not supported)
Postjson.
Upload/download.
4. Application process:
(1) Primary requirements instantiation of a begged manager Afhttprequestoperationmanager.
(2) Set the data format of the begged: acquiescence is binary. (Can not be changed)
Afhttprequestserializer (binary)
Afjsonrequestserializer (JSON)
Afpropertylistrequestserializer (Plist)
(3) Setting the data format for care: acquiescence is JSON. (Can not be changed)
Afhttpresponseserializer (binary)
Afjsonresponseserializer (JSON)
Afpropertylistresponseserializer (Plist)
Afxmlparserresponseserializer (XML)
Afimageresponseserializer (Image)
Afcompoundresponseserializer (combination of)
(4) If the caregiver's mimetype is incorrect, it is necessary to revise the acceptablecontenttypes.
(5) Call the method, send the care of the begged (Get/post ...). Referral website http://www.keymob.com/
For modifying the AFN source code: General Serialization when the support Text/plan and so on, can written off modify the source, in the acceptablecontenttypes can be modified.
AFN for GET, post login:
?
| 123456789101112131415161718192021222324252627282930313233 |
#pragma mark - get/post login- (void) getlogin { //1. Manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager MANAGER];     //2. Setting the login Parameters nsdictionary *dict = @ { @ "username": @ "xn", @ "password": @ "123"  };     //3. Begged [manager get:@ "http://localhost/login.php" parameters:dict success: ^ ( Afhttprequestoperation *operation, id responseobject) { nslog (@ "get --> %@, %@", responseobject, [nsthread CurrentThread]); //active back main thread } failure: ^ (afhttprequestoperation * Operation, nserror *error) { nslog (@ "%@", error); }];} /** * and the Get usage above are identical, only one post parameter is different */- (void) postlogin {  //1. Manager afhttprequestoperationmanager *manager = [ AFHTTPREQUESTOPERATIONMANAGER MANAGER];     //2. Setting Login Parameters nsdictionary *dict = @{ @ "username": @ "xn", @ "password": @ "123" }; //3. Begged [manager post:@ "http://localhost/login.php" parameters:dict success: ^ (Afhttprequestoperation *operation, id responseobject) { nslog (@ "post --> %@, %@", Responseobject, [nsthread currentthread]); //active back main thread } failure: ^ (Afhttprequestoperation *operation, nserror *error) { nslog (@ "%@", error); }];} |
AFN network data analysis, get Plist,json,xml (AFN does not support the active parsing of XML, there is a specific structure to do, such as Sax,pull,kissxml, etc.):
?
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
#pragma mark - get data resolution- (void) getjson { //1. Begged manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager MANAGER];     //2. Recommended [manager get:@ "http://localhost/ Videos.json " parameters:nil success: ^ (afhttprequestoperation *operation, id Responseobject) { nslog (@ "%@", responseobject); } failure: ^ (Afhttprequestoperation *operation, nserror *error) { nslog (@ "%@", error); }];} /** * XML Data resolution */- (void) GETXML {    //1 is not supported. Manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager Manager]; &nBSP;  //2. Set back data type manager.responseserializer = [ afxmlparserresponseserializer serializer]; //first instantiate the      //3. Recommended begged [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. Manager afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];     //2. Setting the response type manager.responseserializer = [ Afpropertylistresponseserializer serializer]; //is response, don't write request . modify to plist type . manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@ "Text/plain"]; / /This can be changed directly into the structure. //3. Begged [manager get:@ "http:// Localhost/videos.plist " parameters:nil success: ^ (afhttprequestoperation *operation, Id responseobject) { nslog (@ "%@", responseObject ); } failure: ^ (afhttprequestoperation *operation, nserror * Error) { nslog (@ "%@", error); }];} |
Use AFN to post JSON data, upload, download, etc.:
?
| 1234567891011121314151617181920212223 |
#pragma mark - post json data with- (void) postjson { //1 such as uploading files. Manager afhttprequestoperationmanager *manager = [ AFHTTPREQUESTOPERATIONMANAGER MANAGER];     //2. Setting Type . ( Here to set the type of Request-response) manager.requestserializer = [ afjsonrequestserializer serializer]; manager.responseserializer = [ afhttpresponseserializer serializer]; //this chooses the type of responseobject back below// manager.responseserializer = [afjsonresponseserializer serializer];// manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@ "Text/plain"];         //2. Setting the login Parameters nsdictionary *dict = @{ @ "username": @ "xn", @ "password": @ "123"  };     //3. Send begged [manager post:@ "http://localhost/postjson.php" parameters:dict success: ^ ( Afhttprequestoperation *operation, id responseobject) {// NSLog (@ "postjson--> %@", responseobject); //so show json. Requirements Settings text/plain nsstring *result = [[nsstring alloc] initwithdata: Responseobject encoding:nsutf8stringencoding]; nslog (@ "%@" , result); } failure: ^ (Afhttprequestoperation *operation, nserror *error) { nslog (@ "%@", error); }];} |
Two ways to start iOS apps