Using the HTTP Network request API in the iOS SDK is quite complex, calls are cumbersome, ASIHTTPRequest is packaged with the Cfnetwork API, and a very simple set of APIs is still available in many older projects, So we still need to be familiar with the use of ASI. Let's study the simple use of ASI together.
First, Environment construction |
1, first download the ASI package , and introduce the following files: http://allseeing-i.com/ASIHTTPRequest/
2. Increase the framework
1, send a synchronous GET request, directly call the Startsynchronous method, the following service address itself can be set up, send the request after the request to call the ResponseData property to get the returned data:
- (void) sync{Nsurl*url=[nsurl urlwithstring:@"http://192.168.2.162/logo.php?userName=jereh&pwd=123"]; ASIHTTPRequest* requst=[[ASIHTTPRequest alloc] initwithurl:url];//setting the time-out periodrequst.timeoutseconds=3; //send a sync request[Requst startsynchronous]; if(requst.error) {NSLog (@"the request went wrong,%@.", Requst.error); }Else{NSData* Data=Requst.responsedata; Nsdictionary* dic=[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@"%@", DIC); }}
2, send the asynchronous GET request, directly call the Startasynchronous method, asynchronous and synchronous slightly different, use the proxy to accept the data
- (void) async{Dataresult=[Nsmutabledata data]; Nsurl*url=[nsurl urlwithstring:@"http://192.168.2.162/logo.php?userName=jereh&pwd=123"]; ASIHTTPRequest* requst=[[ASIHTTPRequest alloc] initwithurl:url]; Self.request=Requst; Requst.timeoutseconds=3; Requst.Delegate=Self ; [Requst startasynchronous]; }//Start Sending Request- (void) requeststarted: (ASIHTTPRequest *) request{}//the call when it fails- (void) requestfailed: (ASIHTTPRequest *) request{NSLog (@"===requestfailed");}//receive data calls, if the file has been called more than once in a few times, it needs to stitch back data- (voidRequest: (ASIHTTPRequest *) Request Didreceivedata: (NSData *) data{[Dataresult appenddata:data];}//parse data after request ends- (void) requestfinished: (ASIHTTPRequest *) request{nsdictionary* dic=[nsjsonserialization jsonobjectwithdata:dataresult options:nsjsonreadingmutablecontainers Error:nil]; }
3, the use of blocks to implement asynchronous get requests, because when faced with multiple requests, the way the proxy is a little bit cumbersome, for example, when stitching data need to determine which request is currently, and then according to the request object address to determine what kind of splicing, so, in the processing of multiple requests with block comparison has the advantage
- (void) asyncformblock{Nsurl*url=[nsurl urlwithstring:@"http://192.168.2.162/loginPost.php"]; Asiformdatarequest* request=[Asiformdatarequest Requestwithurl:url]; //Set Request Parameters[Request Setpostvalue:@"Jereh"Forkey:@"UserName"]; [Request Setpostvalue:@"123"Forkey:@"pwd"]; [Request startasynchronous]; //Parse data after request completes[Request setcompletionblock:^{nsdictionary* dic=[nsjsonserialization jsonobjectwithdata:dataresult options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@"%@", DIC); }]; //call when starting to receive[Request setdatareceivedblock:^ (NSData *data) {[Dataresult appenddata:data]; }]; //the call when it fails[Request setfailedblock:^{ }]; }
4, synchronous POST request implementation, POST request and get different, here need stitching conditions, And the class used this time is slightly different asiformdatarequest, in addition to basically similar. The code is as follows:
asiformdatarequest * Request=[asiformdatarequest Requestwithurl:url]; // set request parameters [Request Setpostvalue: @ " jereh " forkey:@" UserName @ " 123 " forkey:@" PWD =3 ; [Request startsynchronous];
5. Asynchronous POST request I don't have to say that, can I? I'm sure you can write it.
6, download, when it comes to the network is bound to involve the network download and upload, ASI download the file package is very good, we use it very simple, below we through a progress bar shows the download progress, as follows:
- (void) asyncdowload{Nsurl* Url=[nsurl urlwithstring:@"Http://192.168.1.107/test.rar"]; ASIHTTPRequest* request=[ASIHTTPRequest Requestwithurl:url]; NSString* path=[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject];p Ath=[path stringbyappendingpathcomponent:@"Test.rar"]; //Set Download PathRequest.downloaddestinationpath=path; //support for breakpoint continuationrequest.allowresumeforfiledownloads=YES; //set up the download agent and set the proxy as the progress objectRequest.downloadprogressdelegate=Circle; [Request startasynchronous];}
7, file upload and download similar, but need to pay attention to the thing is that the upload needs to send something to the server, since the need to pass things to the server is related to post, get the way is directly obtained, can not pass value therefore, upload needs to use the class Asiformdatarequest, The operation is similar to the post submission mentioned above, but it is important to note that since something needs to be passed, it is necessary to invoke the method of passing the thing. Here we have three ways to
1 Direct settings file, other default
[Request Setfile:path forkey:@ "file"];
2 setting files, setting the destination file name and type
[Request Setfile:path withfilename:@ "Asiimg1.png" andcontenttype:@ "image/png" forkey:@ "file"];
3 Set the binary file, set the destination file name and type
[Request Setdata:data withfilename:@ "T.png" andcontenttype:@ "image/png" forkey:@ "file"];
To learn more about the small partners, you can click to view the source code , run the test yourself.
Inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
ASI simply implements network programming