mysql - Replace NSURLConnection with NSURLSession to download and parse json feed -


i'm beginner in area work database on ios. find way connect mysql database, download , parse json feed. in ios 9, can't use nsurlconnection anymore, that's why have replace nsurlsession. saw many tutorials example this here. far, not able replace it. because i'm under time pressure, can't waste more time this. here me replace it?

my code looks this:

- (void)downloaditems {     // download json file     nsurl *jsonfileurl = [nsurl urlwithstring:@"http://myhost.ch/test.php"];      // create request     nsurlrequest *urlrequest = [[nsurlrequest alloc] initwithurl:jsonfileurl];      // create nsurlconnection     [nsurlconnection connectionwithrequest:urlrequest delegate:self];  }  #pragma mark nsurlconnectiondataprotocol methods  - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response {     // initialize data object     _downloadeddata = [[nsmutabledata alloc] init]; }  - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data {     // append newly downloaded data     [_downloadeddata appenddata:data]; }  - (void)connectiondidfinishloading:(nsurlconnection *)connection {     // create array store locations     nsmutablearray *_locations = [[nsmutablearray alloc] init];      // parse json came in     nserror *error;     nsarray *jsonarray = [nsjsonserialization jsonobjectwithdata:_downloadeddata options:nsjsonreadingallowfragments error:&error];      // loop through json objects, create question objects , add them our questions array     (int = 0; < jsonarray.count; i++)     {         nsdictionary *jsonelement = jsonarray[i];          // create new location object , set props jsonelement properties         location *newlocation = [[location alloc] init];         newlocation.ids = jsonelement[@"idstatistic"];         newlocation.temp = jsonelement[@"temp"];         newlocation.hum = jsonelement[@"hum"];         newlocation.date_time = jsonelement[@"date_time"];          // add question locations array         [_locations addobject:newlocation];     }      // ready notify delegate data ready , pass items     if (self.delegate)     {         [self.delegate itemsdownloaded:_locations];     } } 

you can try this,

{ nsurl *url = [nsurl urlwithstring:@"http://myhost.ch/test.php"];

nsmutableurlrequest *therequest = [nsmutableurlrequest requestwithurl:url];  [therequest sethttpmethod:@"post"];  nsurlsession *session = [nsurlsession sharedsession]; nsurlsessiondatatask *task = [session datataskwithrequest:therequest                                         completionhandler:                               ^(nsdata *data, nsurlresponse *response, nserror *error) {                                    responsedict = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error];                                   nslog(@"result:%@", responsedict);                               }]; [task resume]; 

}


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -