-afnetworking Serialization of iOS network

Source: Internet
Author: User

AFN It internally by default the server response data as JSON to parse, so if the server returned to me is not JSON data then request an error, this time need to set the AFN response information parsing. AFN provides three ways to parse the response information, namely:afjsonresponseserializer---------JSON default mode AFXMLParserResponseSerializer---XML
AFHTTPResponseSerializer---------二进制响应数据
还有一种情况就是服务器返回给我们的数据格式不太一致(开发者工具Content-Type:text/xml),那么这种情况也有可能请求不成功。解决方法: 直接在源代码中修改,添加相应的Content-Type
拿到这个属性,添加到它的集合中
Afjsonresponseserializer Pattern Sample code:
-(void) json{//1. Create a Session Managerafhttpsessionmanager* Manager =[Afhttpsessionmanager Manager]; Nsdictionary*DICTM = @{                            @"username":@"520it",                            @"pwd":@"520it",                            @"type":@"JSON"                            }; //2. Send Get[Manager GET:@"Http://120.25.226.186:32812/login"PARAMETERS:DICTM Progress:nil success:^ (nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {NSLog (@"Success--%@", Responseobject); } Failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {NSLog (@"Failure--%@", error); }];}

AFXMLParserResponseSerializer模式示例代码:

-(void) xml{//1. Create a Session Managerafhttpsessionmanager* Manager =[Afhttpsessionmanager Manager]; Nsdictionary*DICTM = @{                            @"type":@"XML"                            }; Manager.responseserializer=[Afxmlparserresponseserializer Serializer]; //2. Send Get[Manager GET:@"Http://120.25.226.186:32812/video"PARAMETERS:DICTM Progress:nil success:^ (nsurlsessiondatatask * _nonnull task, Nsxmlparser *parser) {        //Set up proxyParser.Delegate=Self ; //Start parsing[parser Parse]; } Failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {NSLog (@"Failure--%@", error); }];}#pragmamark-nsxmlparserdelegate-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary<nsstring *,nsstring *> *) attributedict{NSLog (@"%@---%@", elementname,attributedict);}

AFHTTPResponseSerializerPattern Sample Code:

-(void) http{//1. Create a Session Managerafhttpsessionmanager* Manager =[Afhttpsessionmanager Manager]; NSString*urlstr =@"Http://img4q.duitang.com/uploads/item/201411/21/20141121231731_PkXNk.jpeg"; //How to adjust the resolution of AFN//Manager.responseserializer = [Afjsonrequestserializer serializer];//defaultManager.responseserializer =[Afhttpresponseserializer Serializer]; //2. Send Get[Manager get:urlstr Parameters:nil Progress:nil success:^ (nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {NSLog (@"Success--%@", [Responseobjectclass]); NSLog (@"%@", [Nsthread CurrentThread]); Self.imageView.image=[UIImage Imagewithdata:responseobject]; } Failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {NSLog (@"Failure--%@", error); }];}
-(void) http2{//1. Create a Session Managerafhttpsessionmanager* Manager =[Afhttpsessionmanager Manager]; NSString*urlstr =@"http://www.baidu.com"; //Adjust the resolution of AFN if the error (text/html is provided in the error), 70% or 80% is because of the lack of these two code reasons    manager.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@ "text/html"];
//error domain=com.alamofire.error.serialization.response code=-1016 "Request failed:unacceptable content-type: Text/html ", missing the above code-reported errorManager.responseserializer = [Afhttpresponseserializer serializer];
//error domain=nscocoaerrordomain code=3840 "JSON text does not start with array or object and option to allow Frag ments not set. " , the error is missing from the above code report //2. Send Get[Manager get:urlstr Parameters:nil Progress:nil success:^ (nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {NSLog (@"Success--%@", [Responseobjectclass]); } Failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {NSLog (@"Failure--%@", error); }];}

-afnetworking Serialization of iOS network

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.