Today opened a long time not to write blog, looked at the date, distance from the last blog just one months, this one months, and learned a lot about iOS development knowledge, today to say about the iOS development process of the network request.
About the importance of network requests I don't think I need to say more. For mobile clients, the importance of the network is self-evident. Common network requests have synchronous get, synchronous post, asynchronous get, asynchronous post. Today, let's look at four ways to implement network requests .
One, synchronous get
1. Initialize the URL to an OC string object nsstring *urlstr = [NSString stringwithformat:@ "%@?query=%@®ion=%@&output=json& ak=6e823f587c95f0148c19993539b99295 ", Kbusinessinfourl, @" bank ", @" Jinan "];//if the URL exists in Chinese, do urlencodensstring *newurlstr = [ Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];//2. Building a network URL object, Nsurlnsurl *url = [NSURL urlwithstring:newurlstr];//3. Create a network request nsurlrequest *request = [Nsurlrequest requestwithurl:url cachePolicy: Nsurlrequestreloadignoringlocalcachedata timeoutinterval:10];//Create sync link nsurlresponse *response = nil; Nserror *error = nil; NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];
When you create a sync link, you can parse it in a corresponding way. The same is true for creating asynchronous connections below.
second, synchronous post
1. Initialize OC string object According to URL nsstring *urlstr = [NSString stringwithformat:@ "%@", Kvideourl]; 2. Create Nsurl object nsurl *url = [Nsurl urlwithstring:urlstr]; 3. Create Request nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url]; 4. Create a parameter string object nsstring *parmstr = @ "method=album.channel.get&appkey=mykey&format=json&channel=t &pageno=1&pagesize=10 "; 5. Convert string to NSData object nsdata *pramdata = [Parmstr datausingencoding:nsutf8stringencoding]; 6. Set the request Body [requests sethttpbody:pramdata]; 7. How to set the request [requests sethttpmethod:@ "POST"]; Create a sync link nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];
Three, asynchronous get
NSString *urlstr = [NSString stringwithformat:@ "http://image.zcool.com.cn/56/13/1308200901454.jpg"]; NSString *newstr = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; Nsurl *url = [Nsurl urlwithstring:newstr]; Nsurlrequest *requst = [Nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata TIMEOUTINTERVAL:10]; Asynchronous links (Form 1, less used) [nsurlconnection sendasynchronousrequest:requst queue:[nsoperationqueue Mainqueue] completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) { self.imageView.image = [ UIImage Imagewithdata:data]; Parsing nsdictionary *dic = [Nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@ "%@", dic); }];
Iv. Asynchronous Post
Post request NSString *urlstring = [NSString stringwithformat:@ "%@", Kvideourl]; Create a URL object nsurl *url = [Nsurl urlwithstring:urlstring]; Create request Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy: Nsurlrequestreloadignoringlocalcachedata Timeoutinterval:10]; Create a parameter string object nsstring *parmstr = [NSString stringwithformat:@ "method=album.channel.get&appkey=mykey& Format=json&channel=t&pageno=1&pagesize=10 "]; Converts a string to a NSData object nsdata *data = [Parmstr datausingencoding:nsutf8stringencoding]; [Request Sethttpbody:data]; [Request sethttpmethod:@ "POST"]; Create an asynchronous connection (form two) [nsurlconnection connectionwithrequest:request delegate:self];
In general, when you create an asynchronous connection, the first method is seldom used, and the proxy method is often used. On nsurlconnectiondatadelegate, we often use the Protocol method for a few:
When the server receives the request-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{}// Triggered when the data returned by the server is received, possibly a resource fragment-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{}// triggered when the server returns all data, the data is finished-(void) connectiondidfinishloading: (nsurlconnection *) connection{}//triggered when the request data fails-(void) Connection: (nsurlconnection *) connection didfailwitherror: (Nserror *) error{ NSLog (@ "%s", __function__);}
Finally, analyze the differences between these kinds of network requests.
The difference between a GET request and a POST request:
1. the interface of the GET request contains the parameters section , and the parameters are passed as part of the URL and between the server address and the parameter. to the interval . The POST request separates the server address from the parameter , only the server address in the request interface, and the parameter is submitted to the backend server as part of the request
2. The Get request parameters appear in the interface and are not secure . While POST requests are relatively secure
3. Although both get requests and post requests can be used to request and submit data , General get is used to request data from the background , post More for submitting data to the background
The difference between synchronous and asynchronous :
Synchronous Link : the main thread to request data , when the data request is complete , other threads will not respond, can cause the program on the phenomenon of suspended animation
Asynchronous Link : will open a separate thread to handle the network request , the main thread is still in an interactive state , the program runs smoothly
Network requests in iOS development