Afnetworking is almost the most commonly used HTTP library on iOS, and afnetworking is really easy to use, with a few important APIs, but just a few are enough. Here are a few afnetworking 2.x important APIs.
If you want to support iOS6 then you should use the Afhttprequestoperationmanager based on the Nsurlconnection package (recommended with manager, though AFHTTPRequestOperation也可以 ).
GET request
Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager];//manager.responseserializer=[Afhttpresponseserializer Serializer];//afhttprequestoperationmanager is the JSON response by default and should change the response type if your server is not a JSON response. Using a generic HTTP response is a good choice nsdictionary*parameters = @{@"Q1":@"Query1",@"Q2":@"Query2"};//The parameters requested to the server, that is, the query criteria[Manager GET:@"http://www.yourcompany.com/service/afnetwork.php"Parameters:parameters success:^ (Afhttprequestoperation *operation,IDresponseobject) {//in this place the response of the server is resolved, i.e. Responseobject } failure:^ (Afhttprequestoperation *operation, Nserror *error) {//Handling ErrorsNSLog (@"Error:%@", error);}];
POST request
Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager]; Nsdictionary*parameters = @{@"Q1":@ "Query1@ "Q2": @ "Query2"}; [Manager POST:@"http://www.yourcompany.com/service/afnetwork.php"Parameters:parameters success:^ (Afhttprequestoperation *operation,IDresponseobject) {//Resolve server Response Responseobject, if you want to change the response type description Ibid .} failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"Error:%@", error);}];
If you want to upload nsdata data or files (tablets), you can use the post API as follows
Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager]; Nsdictionary*parameters = @{@"Q1":@"Query1@ "Q2": @ "Query2"}; Nsurl*filepath = [Nsurl Fileurlwithpath:@"File://path/to/image.png"]; [Manager POST:@"http://www.yourcompany.com/service/afnetwork.php"Parameters:parameters constructingbodywithblock:^ (ID<AFMultipartFormData>formData) {[FormData appendpartwithfileurl:filepath name:@"Image"]; //[formData appendpartwitfiledata:imgdata name:@ "image" filename:@ "FileName" mimetype:@ "Image/jpeg"]} success:^ (Afhttprequestoperation *operation,IDresponseobject) { //Handle server-side response Responseobject, whether the response type requires a change of description ibid.} failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"Error:%@", error);}];
If you only support iOS7 and above you can use the Nsurlsession-based API, you can look at the official guide URL Loading System
Create a download task
Nsurlsessionconfiguration *configuration =[Nsurlsessionconfiguration defaultsessionconfiguration];//If you want to continue downloading in the background, you need to configure the background Sessionafurlsessionmanager*manager =[[Afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; Nsurl*url = [Nsurl urlwithstring:@"Http://www.yourcompany.com/service/download.zip"]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; Nsurlsessiondownloadtask*downloadtask = [Manager downloadtaskwithrequest:request progress:nil Destination:^nsurl * (Nsurl *targetPath, Nsurlresponse *response) {Nsurl*documentsdirectoryurl =[[Nsfilemanager Defaultmanager] Urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask Appropriateforurl:nil Create:no Error:nil]; return[Documentsdirectoryurl urlbyappendingpathcomponent:[response suggestedfilename];} Completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {NSLog (@"File downloaded to:%@", FilePath);}]; [Downloadtask resume];//data task to invoke resume
Create an upload task
Nsurlsessionconfiguration *configuration =[Nsurlsessionconfiguration defaultsessionconfiguration]; Afurlsessionmanager*manager =[[Afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; Nsurl*url = [Nsurl urlwithstring:@"http://www.yourcompany.com/service/upload.php"]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; Nsurl*filepath = [Nsurl Fileurlwithpath:@"File://path/to/image.png"]; Nsurlsessionuploadtask*uploadtask = [Manager uploadtaskwithrequest:request fromfile:filepath Progress:nil completionhandler:^ ( Nsurlresponse *response,IDResponseobject, Nserror *error) { if(Error) {NSLog (@"Error:%@", error); } Else{NSLog (@"Success:%@%@", response, Responseobject); }}]; [Uploadtask resume];
Create a Http/json/xml response task
Nsurlsessionconfiguration *configuration =[Nsurlsessionconfiguration defaultsessionconfiguration]; Afurlsessionmanager*manager =[[Afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; Nsurl*url = [Nsurl urlwithstring:@ "http://www.yourcompany.com/service/afnetwork.php"]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; Nsurlsessiondatatask*datatask = [Manager datataskwithrequest:request completionhandler:^ (Nsurlresponse *response,IDResponseobject, Nserror *error) { if(Error) {NSLog (@"Error:%@", error); } Else{NSLog (@"%@ %@", response, responseobject);//responseobject is the json/xml data of the response}]; [Datatask resume];
Of course, in a simpler way, Afhttpsessionmanager provides a similar API to Afhttprequestoperationmanager.
–get:parameters:success:failure:–post:parameters:success:failure:–post:parameters:constructingbodywithblock: Success:failure:
It is called the same way as Afhttprequestoperationmanager.