Analysis and use of iOS network programming afnetwording framework

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.