iOS Development afnetworking

Source: Internet
Author: User

1. Overview

Afnetworking abbreviated as AFN, is one of the mainstream third-party frameworks in iOS development. 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, such as a bunch of problems. And Afnetworking helped us simplify the operation of the network.

2. Several network frameworks earlier

1. 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.


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).

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 Web applications over the HTTP protocol. (This instantiation is not a singleton, because there is no shared word)

Includes creation of request/response serialization/network monitoring/data security.

methods, etc., begin with AF.


3, AFN can do (the network is covered):

Get/post/put/delete/head request.

JSON data parsing/plist data parsing. (XML data parsing is not supported)

Postjson.

Upload/download.


4, the use of the steps:

(1) First you need to instantiate a request manager Afhttprequestoperationmanager.

(2) Set the requested data format: default is binary. (Can not be changed)

Afhttprequestserializer (binary)

Afjsonrequestserializer (JSON)

Afpropertylistrequestserializer (Plist)

(3) Set the data format of the response: JSON is the default. (Can not be changed)

Afhttpresponseserializer (binary)

Afjsonresponseserializer (JSON)

Afpropertylistresponseserializer (Plist)

Afxmlparserresponseserializer (XML)

Afimageresponseserializer (Image)

Afcompoundresponseserializer (combination of)

(4) If the responder's mimetype is incorrect, the acceptablecontenttypes should be modified.

(5) Call the method, send the 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 = [AFHTTPREQUESTOPERATIONMANAGER MANAGER];//2. Setting the 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 --> %@, %@",  responseObject,  [nsthread currentthread]);  //automatically returns to the main thread} failure: ^ (afhttprequestoperation * Operation, nserror *error)  {    nslog (@ "%@",  error);}];} The/** *   is exactly the same as the get usage above,  only one post parameter is 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 --> %@, %@",  responseobject, [nsthread  currentthread]);  //automatically returns to the main thread} failure: ^ (afhttprequestoperation *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 resolution-  (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: ^ (Afhttprequestoperation *operation, nserror *error)  {     nslog (@ "%@",  error);}];} /** *   does not support XML data resolution  */-  (void) getxml {//1. Manager afhttprequestoperationmanager * MANAGER = [AFHTTPREQUESTOPERATIONMANAGER MANAGER];//2. Set the return data type manager.responseserializer =  [afxmlparserresponseserializer serializer]; //first instantiate the//3. initiating a request [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 the request .  Modified to plist type. manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@ "text/ Plain "]; //This can be changed directly inside the frame.//3. Request [manager get:@" Http://localhost/videos.plist " parameters:nil  success: ^ (Afhttprequestoperation *operation, id responseobject)  {     nslog (@ "%@",  responseobject);}  failure: ^ (Afhttprequestoperation *operation, nserror *error)  {     nsloG (@ "%@",  error);}];} 

Post JSON data with AFN, upload, download, etc.:

#pragma  mark - post json data and upload files such as-  (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--> %@",  responseobject);   //you need to set text/plain     to show JSON like this     nsstring *result = [[nsstring alloc] initwithdata: Responseobject encoding:nsutf8stringencoding];        nslog (@ "%@" , result);}  failure: ^ (Afhttprequestoperation *operation, nserror *error)  {     nslog (@ "%@",  error);}];}


iOS Development afnetworking

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.