iOS afnetworking using 1.IOS network Request basics
Before talking about the use of afnetworking, let's briefly introduce the basics of network requests in iOS.
1. Communication Process for network requests
1> Request
* Client-to-server
* Content of the request
A. Request line (Request method \http protocol \ Request Resource Path)
B. Request header (information describing the client)
C. Request body (POST request only needs to have, store specific data)
2> response
* Server-to-client
* Content of the response
A. Status line (response line, status code)
B. Response header (server information, type of return data, length of returned data)
C. Entity content (response body, specific content returned to the client)
2.HTTP method of Request
1> GET
* Parameters are stitched behind the URL
* Parameters are Limited
2> POST
* Parameters are in the request body
* No restrictions on parameters
The difference between 3.GET request and post
(1) is the way HTTP requests, get is generally just to get the data, and post gets to upload additional data at the same time
(2) Get all parameters in URL URLs, POST request parameters are placed in the request header
(3) Application: Simply get data, use GET request, login (pass password), upload file or picture, use POST request
2.AFNetworking use
You can download the latest version of this third-party library to Git, add files to the project, and reference header files to afnetworking
1. GET Request (HTML, JSON, XML)
- (void) testgetrequest{NSString*urlstring =@"http://www.baidu.com"; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; //Note: The default parser considers the data to be JSON, the type that can be resolved Content-type is also json,responseobject is the parsed dictionary or array//FIX: Set parser to HTTP form, download down is NSDataManager.responseserializer =[Afhttpresponseserializer Serializer]; [Manager get:urlstring Parameters:nil success:^ (Afhttprequestoperation *operation,IDresponseobject) {NSString*str =[[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; NSLog (@"str =%@", str); } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"error =%@", error); }];}
2. POST Request
- (void) testpostrequest{//POST:http://quiet.local/testdir/login.php //parameter 1 @ "user": @ "quiet"//parameter 2 @ "password": @ "123"NSString*urlpost =@"http://quiet.local/testdir/login.php"; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; //parameter 1: Incoming address parameter 2:url request parameter, format incoming dictionary//return data also needs to be parsed[Manager Post:urlpost parameters:@{@"User":@"Quiet",@"Password":@"123"} success:^ (Afhttprequestoperation *operation,IDresponseobject) {NSString*returnstr =[[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; NSLog (@"return str =%@", RETURNSTR); } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"error =%@", error); }];}
3. Uploading Files
- (void) testuploadfile{//http://quiet.local/uploadtest/upload.phpNSString *uploadstr =@"http://23.231.10.61/server/"; //parameter 1:imager: The parameter value is a pictureAfhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; [Manager post:uploadstr Parameters:nil Constructingbodywithblock:^(ID<AFMultipartFormData>formData) { //Append the uploaded data to the request bodyNSString *strpath = [[NSBundle mainbundle] Pathforresource:@"Zhongzi"OfType:@"Zip"]; [Strpath stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; Nsurl*url =[Nsurl Fileurlwithpath:strpath]; //initialization of the local path//parameter 1: The key exists as an interface//Parameter 2: file name after uploading//parameter 3:mimetype changes depending on the type of file being uploaded[FormData appendpartwithfileurl:url Name:@"file"FileName:@"Zhongzi3.zip"MimeType:@"Application/x-gzip"Error:nil]; } Success:^ (Afhttprequestoperation *operation,IDresponseobject) {NSString*filestr =[[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; NSLog (@"filestr =%@", FILESTR); } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"error =%@", error); }];}
4. Download the file
- (void) testdownloadfile{NSString*strdownload =@"Http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip"; //Create a Session Manager object (via default configuration)Afurlsessionmanager *manager =[[Afurlsessionmanager alloc] Initwithsessionconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration]]; Nsurlsessiondownloadtask*task = [Manager downloadtaskwithrequest:[nsurlrequest Requestwithurl:[nsurl Urlwithstring:strdownload]] Progress: Nil Destination:^nsurl * (Nsurl *targetpath, Nsurlresponse *response) { //Setting the Save path for a fileNSString *strpath2 =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]; NSString*strpath = [NSString stringWithFormat:@"%@/all.zip", StrPath2]; return[Nsurl Fileurlwithpath:strpath]; //Local Path} completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {NSLog (@"DownLoad Success"); }]; [Task resume];}
Note: The download of the file is done by Nsurlsession subclass Nsurlsessiondownloadtask, so perform the download and remember to start the task---[Task resume]
5. Monitoring network
-(void) testmonitornetworkstatus{ *manager = [[Afhttprequestoperationmanager alloc] Initwithbaseurl:[nsurl urlwithstring:@ "www.baidu.com"]; [Manager.reachabilitymanager setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) { NSLog (@ " status is%@", Afstringfromnetworkreachabilitystatus (status)); }]; [Manager.reachabilitymanager startmonitoring];}
Note: To invoke startmonitoring boot monitoring
Use of iOS afnetworking