Afnetworking using the basic article

Source: Internet
Author: User

1. AFN a set of third-party frameworks for network operations
A. After Nsurlconnection iOS2.0, there are
B. Only after Nsurlsession iOS7.0
C. Default support for serialization and deserialization json,xml needs to parse itself D. Excellent error handling mechanism
E. Encapsulated the reachability

F. Support for HTTPS


Methods of Afhttprequestoperationmanager Initialization

0self.requestserializer = [Afhttprequestserializer serializer]; serialization of request data

0self.responseserializer = [Afjsonresponseserializer serializer]; Serialization of returned data


<afurlrequestserialization>

Data sent to the server
Binary (default)
Json

Plist data received from the server

0AFHTTPRequestSerializer
0AFJSONRequestSerializer
0AFPropertyListRequestSerializer

<AFURLResponseSerialization>
0AFHTTPResponseSerializer
0AFJSONResponseSerializer
0AFXMLParserResponseSerializer
0afxmldocumentresponseserializer0afpropertylistresponseserializer0afimageresponseserializer

0AFCompoundResponseSerializer

Get request with no parameters

- (void) demo1{NSString*str =@"Http://127.0.0.1/demo11.json";//Create a managerAfhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager]; //The Manager initiates the request[Manager Get:str Parameters:nil success:^void(Afhttprequestoperation *ope,IDJSON) {NSLog (@"%@", JSON); } Failure:^void(Afhttprequestoperation *op, Nserror *error) {NSLog (@"%@", error); }];}

Get with parameters

- (void) demo2{NSString*str =@"http://127.0.0.1/php/login.php"; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; //the parameters sentNsdictionary *dic = @{@"username":@"123&ABC",@"Password":@"ABC"}; [Manager get:str Parameters:dic success:^void(Afhttprequestoperation *ope,IDJSON) {NSLog (@"%@", JSON); } Failure:^void(Afhttprequestoperation *op, Nserror *error) {NSLog (@"%@", error); }];}

Post with parameters

- (void) demo3{NSString*str =@"http://127.0.0.1/php/login.php"; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; //the parameters sentNsdictionary *dic = @{@"username":@"123&ABC",@"Password":@"ABC"}; [Manager post:str Parameters:dic success:^void(Afhttprequestoperation *op,IDJSON) {NSLog (@"%@", JSON); } Failure:^void(Afhttprequestoperation *op, Nserror *error) {NSLog (@"%@", error); }];}

Post Upload file

- (void) demo4{NSString*str =@"http://127.0.0.1/upload/upload.php"; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; [Manager post:str Parameters:nil Constructingbodywithblock:^void(ID<AFMultipartFormData>formData) {       //UserFile//Uploading FilesNsurl *fileurl = [[NSBundle mainbundle] Urlforresource:@"swifter.pdf"Withextension:nil]; [FormData appendpartwithfileurl:fileurl Name:@"UserFile"FileName:@"aaa.pdf"MimeType:@"application/pdf"Error:nil]; } Success:^void(Afhttprequestoperation *op,IDJSON) {NSLog (@"%@", JSON); } Failure:^void(Afhttprequestoperation *op, Nserror *error) {NSLog (@"%@", error); }];}

Setting the serializer default for parsing responses is josn if you want to return HTML or other formatting. You must manually set

- (void) demo5{NSString*str =@"http://www.baidu.com"; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; //set the serializer of the parse responseManager.responseserializer =[Afhttpresponseserializer Serializer]; [Manager get:str Parameters:nil success:^void(Afhttprequestoperation *op,IDdata) {NSString*html =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; NSLog (@"%@", HTML); } Failure:^void(Afhttprequestoperation *op, Nserror *error) {NSLog (@"%@", error); }];}

   JOSN code returned in HTML format

- (void) demo7{NSString*str =@"http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html"; Afhttpsessionmanager*manager =[Afhttpsessionmanager Manager]; Manager.responseSerializer.acceptableContentTypes=[nsset setwithobjects:@"Application/json",@"Text/json",@"Text/javascript",@"text/html", nil]; [Manager get:str Parameters:nil success:^void(Nsurlsessiondatatask *op,IDdata) {NSLog (@"%@", data); } Failure:^void(Nsurlsessiondatatask *op, Nserror *error) {NSLog (@"%@", error); }]; }

Afnetworking using the basic article

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.