As we all know, Apple's set of frameworks nscontention send requests and receive requests in a cumbersome way. It is inconvenient to operate. Not only to distinguish the various requests to set a variety of parameters , but also often in multi-threaded operation , but also to the request and return data to do a variety of serialization operations , but also to consider the security of the request data Wait a bunch of questions.
I. Several earlier network frameworks1. ASI Framework: HTTP Terminator. Very good, but there is a bug, has stopped the update. 2, Mknetworkkit (written by the Indians). 3, AFN has been updated.
The advent of afnetworking: A set of network frameworks designed by Mac/ios. (To simplify network operations)
Address: https://github.com/AFNetworking/AFNetworking
*AFN focuses on network data transfer, as well as multi-threading in the network.
Second, the use of afnetworking
1, AFN characteristics:* login parameters, the dictionary can be passed. (The key name is the parameter name and the key value is the argument value). * Automatically executes in a child thread and returns to the main thread after execution. * The returned results are automatically serialized as Nsdictionary. 2. Use AFN note:* Afhttprequestoperationmanager encapsulates a common method of communicating with a Web application over an HTTP protocol. (This instantiation is not a singleton, because there is no shared word) * Includes create request/response serialization/network monitoring/Data security. * methods, etc. are all beginning with AF.
3, AFN can do (all the network is covered):* Get/post/put/delete/head request. *json Data parsing/plist data parsing. (XML data parsing not supported) *postjson.* upload/download.
4, the use of the steps: (can refer to the documentation) 1. You first need to instantiate a request manager afhttprequestoperationmanager.2. Set the requested data format: default is binary. (not possible) *afhttprequestserializer (binary) *afjsonrequestserializer (JSON) *afpropertylistrequestserializer (Plist) 3. Set the data format for the response: JSON is the default. (Not available) *afhttpresponseserializer (binary) *afjsonresponseserializer (JSON) *afpropertylistresponseserializer (Plist) * Afxmlparserresponseserializer (XML) *afimageresponseserializer (Image) *afcompoundresponseserializer (combined) 4. If the responder's mimetype is incorrect, it is necessary to modify the Acceptablecontenttypes.5. Call method, send a response request (Get/post ...).
about modifying the AFN source code: usually serialized when the support Text/plan, etc., you can modify the source once and for all, modify in Acceptablecontenttypes.
AFN for GET, post login:
#pragma mark-get/post Login-(void) GetLogin {//1. Manager afhttprequestoperationmanager *manager = [Afhttprequestoperationmana GER Manager]; 2. Set login parameters Nsdictionary *dict = @{@ "username": @ "xn" @ "password": @ "123"}; 3. Request [Manager get:@ "http://localhost/login.php" parameters:dict success: ^ (afhttprequestoperation *operation, ID Responseobject) {NSLog (@ "GET-and%@,%@", Responseobject, [Nsthread CurrentThread]);//auto-return main thread} failure: ^ (afhttprequ Estoperation *operation, Nserror *error) {NSLog (@ "%@", error);}]; }/** * is exactly the same as the get usage above, with only one post parameter different */-(void) Postlogin {//1. Manager afhttprequestoperationmanager *manager = [Afhttpreque Stoperationmanager Manager]; 2. Set login parameters Nsdictionary *dict = @{@ "username": @ "xn" @ "password": @ "123"}; 3. Request [Manager post:@ "http://localhost/login.php" parameters:dict success: ^ (afhttprequestoperation *operation, ID Responseobject) {NSLog (@ "POST-and%@,%@", Responseobject, [Nsthread CurrentThread]);//auto-return main thread} failure: ^ (afhttpreq Uestoperation *Operation, Nserror *error) {NSLog (@ "%@", error);}]; }
AFN Network data parsing, get Plist,json,xml(AFN does not support automatic parsing of XML, there is a special framework to do, such as sax,pull,kissxml, etc.)
#pragma mark-get data parsing-(void) Getjson {//1. Request Manager Afhttprequestoperationmanager *manager = [afhttprequestoperationmanage R Manager]; 2. Initiating the request [manager get:@ "Http://localhost/videos.json" Parameters:nil success: ^ (afhttprequestoperation *operation, ID Responseobject) {NSLog (@ "%@", Responseobject);} failure: ^ (afhttprequestoperation *operation, Nserror *error) {NSLog (@ "%@", error); }]; }/** * does not support XML data parsing */-(void) GetXML {//1. Manager afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager ma Nager]; 2. Set the return data type Manager.responseserializer = [Afxmlparserresponseserializer serializer]; Instantiate the//3 first. initiating a request [manager get:@ "Http://localhost/videos.xml" Parameters:nil success: ^ (Afhttprequestoperation * operation, id responseobject) {NSLog (@ "%@", Responseobject);} failure: ^ (afhttprequestoperation *operation, Nserror *er ROR) {NSLog (@ "%@", error);}]; }-(void) Getplist {//1. Manager afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager]; 2. Set ResponSE type manager.responseserializer = [Afpropertylistresponseserializer serializer]; It's response, don't write the request. Modified to plist type. Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@ "Text/plain"]; This can be changed directly inside the frame. | Search for Xuneng in the project and locate the anchor point. 3. Request [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. (Upload, download page description on the https://github.com/AFNetworking/AFNetworking)
#pragma mark-post json data to upload files, etc.-(void) Postjson {//1. Manager afhttprequestoperationmanager *manager = [Afhttprequestoperati Onmanager Manager]; 2. Set the type. (here to set the type of request-response) Manager.requestserializer = [Afjsonrequestserializer serializer]; Manager.responseserializer = [Afhttpresponseserializer serializer]; This determines the type of responseobject returned below//Manager.responseserializer = [Afjsonresponseserializer serializer]; Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@ "Text/plain"]; 2. Set login parameters Nsdictionary *dict = @{@ "username": @ "xn" @ "password": @ "123"}; 3. Send Request [manager post:@ "http://localhost/postjson.php" parameters:dict success: ^ (Afhttprequestoperation *operation , id responseobject) {//nslog (@ "postjson-->%@", responseobject);//This will require setting text/plain nsstring *result = [[NSS Tring Alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; NSLog (@ "%@", result); } failure: ^ (afhttprequestoperation *operation, Nserror *error) {NSLog (@ "%@", error); }]; }
Network operations and afnetworking