Today, I'll talk about network requests during iOS development.
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 into an OC string object
NSString *urlstr = [NSString stringwithformat:@ "%@?query=%@®ion=%@&output=json&ak= 6e823f587c95f0148c19993539b99295 ", Kbusinessinfourl, @" bank ", @" Jinan "];
If there is Chinese in the URL, do UrlEncode
NSString *newurlstr = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
2. Build the Network URL object, Nsurl
Nsurl *url = [Nsurl urlwithstring:newurlstr];
3. Create a network request
Nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata TIMEOUTINTERVAL:10];
Create a 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 based on URL
NSString *urlstr = [NSString stringwithformat:@ "%@", Kvideourl];
2. Create a Nsurl Object
Nsurl *url = [Nsurl urlwithstring:urlstr];
3. Create a request
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];
4. Creating a Parameter String object
NSString *parmstr = @ "method=album.channel.get&appkey=mykey&format=json&channel=t&pageno=1& Pagesize=10 ";
5. Convert a string to a NSData object
NSData *pramdata = [Parmstr datausingencoding:nsutf8stringencoding];
6. Set the request body
[Request Sethttpbody:pramdata];
7. How to set the request
[Request 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];
Analytical
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];
Creating a Parameter String object
NSString *parmstr = [NSString stringwithformat:@ "method=album.channel.get&appkey=mykey&format=json& Channel=t&pageno=1&pagesize=10 "];
Convert 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 is returned
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data
{
}
triggered when the server returns all data, the data is returned to completion
-(void) connectiondidfinishloading: (nsurlconnection *) connection
{
}
triggered when 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. The POST request is 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, and post is used to submit 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 are not responding, will 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