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.ASIFrame: HTTPEnd-User.It's bull .,but there areBugs,Update has stopped.2.Mknetworkkit (written by the Indians.).3.AFNIs still being updated.
The advent of afnetworking: a set of network frameworks designed by Mac/ios . ( to Streamline Network Operations )
Address: https://github.com/AFNetworking/AFNetworking
*AFNFocus andNetwork Data transfer,and the networkMultithreadingthe processing.
Second, the use of afnetworking
1.AFNfeatures:*when logging in to pass parameters,PassDictionarycan be. (Key Name is parameter name,key values are parameter values).*automatically executes in a child thread,return after executionback to main thread.*The returned resultAutomatic Serializationto bensdictionary.
2.UseAFNNote :*Afhttprequestoperationmanagerencapsulated by theHTTPAgreement andWebcommon ways to communicate with applications.(when this instantiation isnot a single case,because there is nosharedWord)*includeCreate request/Response Serialization/Network Monitoring/Data Security.*methods, etc. areinAFbegins with.
3.AFNcan do. (all of the networks are covered by):*GET/Post/put/delete/headRequest.*JSONData Parsing/PlistData Parsing. (not supportedXMLData Parsing)*POSTJSON.*Upload/Download.
4.Use steps: (refer to documentation)1.First, you needinstantiation ofa request managerAfhttprequestoperationmanager.2.SetRequestthe data format:default is binary.(not to be changed .)*Afhttprequestserializer ( Binary)*Afjsonrequestserializer (JSON)*Afpropertylistrequestserializer (Plist)3.SetResponsethe data format:default isJSON.(not to be changed .)*Afhttpresponseserializer ( Binary)*Afjsonresponseserializer (JSON)*Afpropertylistresponseserializer (Plist)*Afxmlparserresponseserializer (XML)*Afimageresponseserializer (Image)*Afcompoundresponseserializer (combination of)4.if the responder'sMIMETypenot correct,need to modifyAcceptablecontenttypes.5.Calling Methods,Sendresponse ofRequest(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 = [ Afhttprequestoperationmanager 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: ^ (afhttpre Questoperation *operation, Nserror *error) {NSLog (@ "%@", error);}];} /** * is exactly the same as the get usage above, with only one post parameter */-(void) Postlogin {//1. Manager Afhttprequestoperationmanager *manager = [ Afhttprequestoperationmanager 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: ^ (AFHTTPR Equestoperation *operation, NSERror *error) {NSLog (@ "%@", error);}];}
AFN Network data analysis, get Plist,json,xml(AFN does not support parsing XML automatically, 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 = [ Afhttprequestoperationmanager MANAGER];//2. Initiating a request [manager get:@ "Http://localhost/videos.json" Parameters:nil success : ^ (afhttprequestoperation *operation, id responseobject) {NSLog (@ "%@", Responseobject);} failure: ^ (afhttprequestoper ation *operation, Nserror *error) {NSLog (@ "%@", error);}];} /** * does not support XML data parsing */-(void) GetXML {//1. Manager afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager MANAGER];//2. Setting 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 *e Rror) {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, locate the anchor point.//3. Request [Manager get:@ "Http://localhost/videos.plist" Parameters:nil success: ^ ( Afhttprequestoperation *operation, id responseobject) {NSLog (@ "%@", Responseobject);} failure: ^ (Afhttprequestoperati On *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 = [ Afhttprequestoperationmanager MANAGER];//2. Set 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--& Gt %@ ", responseobject); To display JSON, you need to set text/plain nsstring *result = [[NSString alloc] Initwithdata:responseobject Encoding:nsutf8stringen Coding]; NSLog (@ "%@", result);} Failure: ^ (afhttprequestoperation *operation, Nserror *error) {NsloG (@ "%@", error);}];}
Reprint Please specify source: http://blog.csdn.net/xn4545945
"IOS" Network operation and Afnetworking