#pragma get synchronization:
Synchronous network requests are not recommended in development
-(Ibaction) Gett: (ID) Sender {//the method obtained through storyboard association
Address string
NSString *urlstring = @ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date= 20131129&startrecord=1&len=5&udid=1234567890&terminaltype=iphone&cid=213 ";
Uniform Resource Locator (URL)
Nsurl *url = [Nsurl urlwithstring:urlstring];
First parameter: Uniform Resource Locator (URL)
Second parameter: Cache policy
Third parameter: Time-out
Nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: (nsurlrequestreloadignoringlocalcachedata) TIMEOUTINTERVAL:60.0F];
Nsurlconnection is not recommended after iOS 9.0
First parameter: object of type Nsurlrequest
The second parameter: the information stored is some network request, generally nil (package body)
Third parameter: Error message
NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];
NSLog (@ "data=====%@", data);
Parsing data
Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:data options: (nsjsonreadingallowfragments) Error:nil];
NSLog (@ "dic===%@", DIC);
}
/*
Nsurlrequestuseprotocolcachepolicy
: underlying cache policy
* Nsurlrequestreloadignoringlocalcachedata
: Ignore local cache
* Nsurlrequestreloadignoringlocalandremotecachedata
: Ignore local cache, regardless of local cache, are downloaded from the network
Nsurlrequestreloadignoringcachedata=nsurlrequestreloadignoringlocalcachedata,
: If the local cache is valid, do not download it.
Nsurlrequestreturncachedataelseload
: Prefer to use local cache, if there is no cache locally, go to download
* Nsurlrequestreturncachedatadontload
: Never download, only use local cache, if not, the request fails. More for offline environments
* * Nsurlrequestreloadrevalidatingcachedata
: Verify that local data and network data are the same, if different, go to download, if same, use local cache
*/
#pragma post synchronization:
-(Ibaction) POSTT: (ID) Sender {
NSString *urlstring = @ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
Nsurl *url = [Nsurl urlwithstring:urlstring];
Nsmutableurlrequest *urlrequest = [Nsmutableurlrequest Requestwithurl:url];
Create a string
NSString *datastring = @ "Date=20131129&startrecord=1&len=5&udid=1234567890&terminaltype=iphone &cid=213 ";
Convert this string to an object of type NSData
NSData *postdata = [datastring datausingencoding:nsutf8stringencoding];
Set the request way in uppercase such as: POST GET
The GET request can not be set, but the POST request must be set
[URLRequest sethttpmethod:@ "POST"];
Put the data you need to upload into urlrequest.
[URLRequest Sethttpbody:postdata];
NSData *resultdata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:nil Error:nil];
Nsdictionary *dic1 = [nsjsonserialization jsonobjectwithdata:resultdata options: (nsjsonreadingallowfragments) Error: NIL];
NSLog (@ "dic1====%@", Dic1);
}
#pragma get async:
The first type:
-(Ibaction) GetY: (ID) Sender {
NSString *string = @ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?";
Nsurl *url = [Nsurl urlwithstring:string];
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
Call a method with a block callback to perform a network request
First parameter: nsurlrequest
Second parameter: in which thread the code inside the block blocks executes
[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror * _nullable connectionerror) {
Response is carrying some information about the interface.
Data that is requested to be used by the
Connectionnnerror error message
if (Connectionerror = = nil) {
Nsdictionary *dic2 = [nsjsonserialization jsonobjectwithdata:data options: (nsjsonreadingallowfragments) Error:nil];
NSLog (@ "dic2====%@", Dic2);
The UI should be refreshed here
1. Assigning a value to an array of data sources
2; Refresh UI ([Self.tableview Reloaddata]) after assignment ends
}
}];
}
The second method of
-(Ibaction) Get: (ID) Sender {
How to use block callbacks
Using the system-provided global Nsurlsession object, is a single case
Nsurlsession *session = [Nsurlsession sharedsession];
NSString *string = @ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date= 20131129&startrecord=1&len=5&udid=1234567890&terminaltype=iphone&cid=213 ";
Nsurl *url = [Nsurl urlwithstring:string];
Nsurlsession is task-based, so everything is put into the task, Nsurlsessiontask is Nsurlsession's task execution object
Nsurlsessiontask *task = [Session Datataskwithurl:url completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nulla BLE response, Nserror * _nullable error) {
if (Error = = nil) {
Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:data options: (nsjsonreadingallowfragments) Error:nil];
NSLog (@ "dic===%@", DIC);
}
}];
All nsurlsession tasks are suspended by default, so be sure to call the Resume method to start the task
[Task resume];
}
//The third method follows Nsurlsessiondatadelegate nsurlsession the proxy protocol for obtaining network data
Asynchronous operation of the nsurlsession agent
The properties of the Nsurlsession agent are read-only
First parameter: Session mode
Second parameter: Agent
Third argument: The proxy method is in that thread.
Nsurlsession *session1 = [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration] delegate:self delegatequeue:[nsoperationqueue Mainqueue]];
Nsurl *url = [Nsurl urlwithstring:@ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/ Apinewslist.ashx?date=20131129&startrecord=1&len=5&udid=1234567890&terminaltype=iphone&cid =213 "];
Sub-class object for Nsurlsessiondatatask
Nsurlsessiondatatask *datatask = [Session1 Datataskwithurl:url];
[Datatask resume];
}
Server starts responding
-(void) Urlsession: (Nsurlsession *) session Datatask: (Nsurlsessiondatatask *) Datatask Didreceiveresponse: ( Nsurlresponse *) Response Completionhandler: (void (^) (nsurlsessionresponsedisposition)) Completionhandler {
Completionhandler (Nsurlsessionresponseallow);
Self.resultdata = [Nsmutabledata data];
}
Receive the data
-(void) Urlsession: (Nsurlsession *) session Datatask: (nonnull nsurlsessiondatatask *) datatask didreceivedata: (nonnull NSData *) Data {
[Self.resultdata Appenddata:data];
}
End response
-(void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didcompletewitherror: (Nserror *) Error {
if (Error = = nil) {
Nsdictionary *dict = [nsjsonserialization JSONObjectWithData:self.resultdata options: (nsjsonreadingallowfragments) Error:nil];
NSLog (@ "Dictionary =%@", dict);
}
}
//Fourth method : Follow the Nsurlconnectiondatadelegate protocol
#pragma post async:
//The first type : follow the Nsurlconnectiondatadelegate protocol
-(Ibaction) Posty: (ID) Sender {
NSString *string = @ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?";
Nsurl *url = [Nsurl urlwithstring:string];
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];
[Nsurlconnection connectionwithrequest:request delegate:self];
NSString *datastr = @ "date=20131129&startrecord=1&len=5&udid=1234567890&terminaltype=iphone& cid=213 ";
NSData *postdata = [Datastr datausingencoding:nsutf8stringencoding];
[Request sethttpmethod:@ "POST"];
[Request Sethttpbody:postdata];
}
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{
Self.resultdata = [Nsmutabledata data];
}
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{
[Self.resultdata Appenddata:data];
}
-(void) connectiondidfinishloading: (nsurlconnection *) connection{
Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:_resultdata options: (nsjsonreadingallowfragments) Error: NIL];
NSLog (@ "%@", DIC);
}
//second type : Session
-(Ibaction) post: (ID) Sender {
Nsurlsession *session = [Nsurlsession sharedsession];
NSString *str = @ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?";
Nsurl *url = [Nsurl urlwithstring:str];
Nsmutableurlrequest *urlrequest = [Nsmutableurlrequest Requestwithurl:url];
NSString *datastring = @ "Date=20131129&startrecord=1&len=5&udid=1234567890&terminaltype=iphone &cid=213 ";
NSData *pdata = [datastring datausingencoding:nsutf8stringencoding];
[URLRequest sethttpmethod:@ "POST"];
[URLRequest Sethttpbody:pdata];
Nsurlsessiontask *task = [Session datataskwithrequest:urlrequest completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {
if (Error = = nil) {
Nsdictionary *dic1 = [nsjsonserialization jsonobjectwithdata:data options: (nsjsonreadingallowfragments) Error:nil];
NSLog (@ "%@", Dic1);
}
}];
[Task resume];
}
The third type:
-(Ibaction) POSTT: (ID) Sender {
NSString *urlstring = @ "Http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
Nsurl *url = [Nsurl urlwithstring:urlstring];
Nsmutableurlrequest *urlrequest = [Nsmutableurlrequest Requestwithurl:url];
Create a string
NSString *datastring = @ "Date=20131129&startrecord=1&len=5&udid=1234567890&terminaltype=iphone &cid=213 ";
Convert this string to an object of type NSData
NSData *postdata = [datastring datausingencoding:nsutf8stringencoding];
Set the request way in uppercase such as: POST GET
The GET request can not be set, but the POST request must be set
[URLRequest sethttpmethod:@ "POST"];
Put the data you need to upload into urlrequest.
[URLRequest Sethttpbody:postdata];
NSData *resultdata = [nsurlconnection sendasynchronousrequest:urlrequest returningresponse:nil Error:nil];
Nsdictionary *dic1 = [nsjsonserialization jsonobjectwithdata:resultdata options: (nsjsonreadingallowfragments) Error: NIL];
NSLog (@ "dic1====%@", Dic1);
}
//Fourth: Agent protocol for obtaining network data following Nsurlsessiondatadelegate nsurlsession
Get,post synchronous and asynchronous all-in-code method