Network request -get Request:
1,nsurl: request address;
2,nsurlrequest: a nsurlrequest object represents a request that contains information such as:
1) A nsurl object
get request, do not need to write the request header, the request body, just tell the request path and request parameters can be.
2) Request method
3) Request timed out
3, sub-class of Nsmutableurlrequest:nsurlrequest
4,nsurlconnection:
Responsible for sending the request, establish the client and server connection;
Nsurlconnection steps to use
1, create a nsurl object, set the request path
//URL cannot contain Chinese
Nsurl *url = [Nsurl urlwithstring:urlstr];
2, incoming nsurl create a nsurlrequest object, set the request header and the request body
2.2. Create Request Object
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; The default is a GET request
Request.timeoutinterval = 5; Set Request Timeout
3, send nsurlrequest using nsurlconnection
Nsurlconnection *conn = [nsurlconnection connectionwithrequest:request delegate:self];
Nsurlconnection Sending requests
1, sync request
[Nsurlconnection sendsynchronousrequest:<# (Nsurlrequest *) #> returningresponse:<# (NSURLResponse *__ autoreleasing *) #> error:<# (nserror *__autoreleasing *) #>];
2, asynchronous request : According to the way the server returns data processing, can be divided into 2 different
1)block callback:
[Nsurlconnection sendasynchronousrequest:<# (Nsurlrequest *) #> queue:<# (Nsoperationqueue *) #> completionhandler:<#^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) handler#>];
2) Agent :
Nsurlconnection *conn1 = [[Nsurlconnection alloc] initwithrequest:<# (nsurlrequest *) #> delegate:<# (ID) #>] ;
Nsurlconnection *conn1 = [[Nsurlconnection alloc] initwithrequest:<# (nsurlrequest *) #> delegate:<# (ID) #> startimmediately:<# (BOOL) #>];
Nsurlconnection *conn = [nsurlconnection connectionwithrequest:request delegate:self];
In this case, you need to call the Start method to start sending the request
-(void) start;
become Nsurlconnection Agent, the best to abide by the nsurlconnectiondatadelegate agreement;
JSON:
What is JSON:
1 , JSON is a lightweight data format that is typically used for data interaction;
2, the data returned to the client by the server, usually in JSON format or XML format (except for file download)
The format of JSON is much like the dictionary and array in OC
{"Name": "Jack", "Age": 10}
{"Names": ["Jack", "Rose", "Jim"]}
Note points in the standard JSON format:key must be double-quoted
JSON Parsing scheme:
1, in IOS , there are 4 common parsing methods forJSON
third-party frameworks: Jsonkit, sbjson, Touchjson ( performance from left to right, the worse);
Apple Native (comes with): Nsjsonserialization ( Best performance );
2, Common methods of nsjsonserialization
OC Object , JSON data
[Nsjsonserialization jsonobjectwithdata:<# (NSData *) #> options:<# (nsjsonreadingoptions) #> error:<# ( Nserror *__autoreleasing *) #>];
OC Object --JSON data
[Nsjsonserialization datawithjsonobject:<# (ID) #> options:<# (nsjsonwritingoptions) #> error:<# ( Nserror *__autoreleasing *) #>];
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Get Request for Network request