In the first few years, I introduced the concept and basic use of ASI in detail. If you want to know, please click here. Since there are many similarities between AFN and ASI, I intend to introduce AFN in a section. : https://github.com/AFNetworking/AFNetworking
The main structure of AFN is Afnetworiking,afn as follows:
1:afhttpclient
Provides a convenient network interface, including the default header, authentication, whether to connect to the network, batch processing operations, query string parameter serialization, already multiple form requests
2:afhttprequestoperation
And its subclass can be based on the HTTP status and content column down to distinguish whether the request was successful
3:afurlconnectionoperation
And its subclasses inherit Nsoperation, allowing requests to be canceled, paused/resumed, and managed by Nsoperationqueue.
4:afurlconnectionoperation
Allows you to easily complete uploads and downloads, process validation, monitor upload and download progress, and control the cache.
Introduction to use:
1.GET request
#pragma mark-The JSON data returned by the server (Example: Call Weather Interface)-(void) jsonresponse{afhttprequestoperationmanager *manager = [ Afhttprequestoperationmanager Manager]; The default responseserializer is Afjsonresponseserializer, so the following sentence can be used without writing//Manager.responseserializer = [ Afjsonresponseserializer Serializer]; Async thread [Manager get:@ "Http://apis.haoservice.com/weather" parameters:@{@ "CityName": @ "Shanghai"} success:^ ( Afhttprequestoperation *operation, id responseobject) {//If the returned response is JSON data, then the resulting responseobject is a dictionary or an array NSLog (@ "result:%@", responseobject); } failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@ "error:%@", error); }];} #pragma mark-Want to return HTML data (Example: Search in Baidu "Shanghai")-(void) commonresponse{afhttprequestoperationmanager *manager = [ Afhttprequestoperationmanager Manager]; Manager.responseserializer = [Afhttpresponseserializer serializer]; Async thread [Manager get:@ "Http://baike.baidu.com/link" parameters:@{@ "url": @ "DioqvUurnw8hemttg0vhitjhuceubugs82tyyklr_p2uybhtx7rm7t9rtricuqvpr6qmativos2fhalm2etgkq "} success:^ ( Afhttprequestoperation *operation, id responseobject) {//If the returned response is Afhttpresponseserializer information, then the resulting re Sponseobject can be converted to string display nsstring *result = [[NSString alloc] Initwithdata:responseobject Encoding:nsutf8stringenc Oding]; NSLog (@ "result:%@", Result); } failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@ "error:%@", error); }];}
2. POST request
Afhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager]; Non-file parameters, such as: User name, password and other information nsmutabledictionary *params = [nsmutabledictionary dictionary]; params[@ "username"] = @ "Jason"; params[@ "password"] = @ "123"; [Mgr post:@ "PostURL" Parameters:params constructingbodywithblock:^ (id<afmultipartformdata> formData) { //Be sure to add a file parameter to the block nsstring *name = [[NSBundle mainbundle] pathforresource:@ "Icon" oftype:@ "png"]; NSData *data = [NSData datawithcontentsoffile:name]; Name: is the background server receives the upload file corresponding parameter [formData appendpartwithfiledata:data name:@ "test.txt" filename:@ "file" mimetype:@ " Text/plain "]; } success:^ (afhttprequestoperation *operation, id responseobject) { } failure:^ (Afhttprequestoperation *operation , Nserror *error) { }];
At this point, I have already introduced ASI and AFN, the following compare them, we consider the choice of who ...
1. Principle Analysis
Observation of the above structure can be seen: ASI based on Cfnetwork framework development, and AFN based on Nsurl, the underlying difference is one of the important reasons for the performance gap between the two. Theoretically,ASI has better performance than AFN.
2. When the version is submitted:
AFN's first submission was January 1 of 2011, when ASI was already a 1.8+ version, and when AFN released version 1.0, October 2012, the ASI had stopped updating early. So it seems that AFN is the successor to ASI, and there seems to be no problem with the difficult choice mentioned before.
Finally, we introduce a derivative product: reachability (used to determine the status of the current network)
[[reachability reachabilityforinternetconnection] currentreachabilitystatus]
The above sentence is used to get the current network state, which returns an enumeration value
enum {//Apple networkstatus Constant names.notreachable = Knotreachable,reachableviawifi = Kreachableviawifi, Reachableviawwan = Kreachableviawwan};
AFN Usage Introduction