Known. Apple's set of frameworks nscontention send requests and receive requests in a cumbersome way. It is very inconvenient to operate.
Not only to distinguish between various requests to set a variety of parameters , and also often in multi-threaded operation , at the same time, the request and the returned data to do a variety of serialization operations . At the same time, you have to consider the security of the request data and a bunch of problems.
I. Several earlier network frameworks1.ASIFrame: HTTPEnd-User.very cow .,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 Transfer data,and the networkMultithreadingthe processing.
Second, the use of afnetworking
1.AFNfeatures:*when you log in to the number of passes,PassDictionary'll be. (Key Name is the name of the participant,key value is the number of parameters).*Take the initiative to run in a child thread,return after runningback to main thread.*The returned resultself-active serializationto bensdictionary.
2.UseAFNNote :*Afhttprequestoperationmanagerencapsulated by theHTTPAgreement andWebfrequent usage of applications for communication.(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 need instantiation of a request manager Afhttprequestoperationmanager. 2. Set Request the data format : default is binary . ( can not be changed ) *Afhttprequestserializer ( Binary)*Afjsonrequestserializer (JSON)*Afpropertylistrequestserializer (Plist) 3. Set Response the data format : default is JSON. ( can not be changed ) *Afhttpresponseserializer ( Binary)*Afjsonresponseserializer (JSON)*Afpropertylistresponseserializer (Plist)*Afxmlparserresponseserializer (XML)*Afimageresponseserializer (Image)*Afcompoundresponseserializer (combination of) 4. assume that the responder's MIMEType is not correct , it is necessary to change Acceptablecontenttypes. 5. Calling Methods , Send response of Request (Get/post ...).
about changing AFN source code : When you normally serialize support for Text/plan, and so on. You can change the source code once and for all, and change it 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]);//self-actively return to the main thread} failure: ^ (afhttp Requestoperation *operation, Nserror *error) {NSLog (@ "%@", error);}];} /** * and the above get using the same method, there is only one post number of different */-(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-to%@,%@", Responseobject, [Nsthread CurrentThread]);//self-actively return to main thread} failure: ^ (afhtt Prequestoperation *operatiOn, Nserror *error) {NSLog (@ "%@", error);}];}
AFN for network data analysis. Gets the Plist,json. XML(AFN does not support its own active 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 = [ 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. Change to plist type. manager.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@ "Text/plain"]; This can be changed directly into the frame.//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 following Responseobject return type//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