At present, it may be simply affixed to some of the demo code, but the code is the request data should be the most basic use of it, in the actual development of the project may be used in the system will be very few, generally using others very mature third-party open Source Library to achieve data requests, The current common third-party network requests are mainly the following:
#pragma mark-NetWorking methods//are get synchronous and asynchronous-(void) gethttprequest{nsstring *urlstring = [NSString stringwith format:@ "Http://api.jiepang.com/v1/locations/search?lat=%f&lon=%f&source=100000&count=50", 30.575413,104.064359]; Nsurl *url = [Nsurl urlwithstring:urlstring]; Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; /* Nserror *error = nil; Here is the publish synchronization request, the synchronization request blocks the main thread, the program is unable to interact during the request is not completed nsdata *data = [Nsurlconnection sendsynchronousrequest:request Returningresponse:nil error:&error]; if (Error) {NSLog (@ "Request error reason '%@ '", [Error localizeddescription]); }else {//For data parsing, here is the system comes with the JSON parsing id objId = [nsjsonserialization jsonobjectwithdata:data options:nsjsonrea Dingmutablecontainers Error:nil]; _weatherlabel.text = objid[@ "Result"][@ "Today"][@ "weather"]; NSLog (@ "%@", objId); }///Here is a proxy method to implement an asynchronous request to obtain data [nsurlconnection connectionwithrequest:request deLegate:self];} Post asynchronous request-(void) posthttprequest{nsurl *url = [Nsurl Urlwithstring:oil_price]; The parameters are processed nsmutabledictionary *params = [[Nsmutabledictionary alloc]init]; [Params setobject:@ "5a43794d639815615b93fdfddcef3670" forkey:@ "key"]; nsmutablestring *mstring = [nsmutablestring string]; For (NSString *key in [params AllKeys]) {[Mstring appendformat:@ "%@=%@", Key,params[key]]; }//POST request requires a variable urlrequest nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; Request. Httpbody = [mstring datausingencoding:nsutf8stringencoding]; Request parameters to encode requests. HttpMethod = @ "POST"; Request Mode Request.timeoutinterval = 10;//Request Time-out [nsurlconnection connectionwithrequest:request delegate:self]; }
<pre name= "code" class= "OBJC" > #pragma mark--<nsurlconnectiondelegate>//Request Complete Call this method-(void) Connectiondidfinishloading: (nsurlconnection *) connection{ nserror *error = nil; ID ObjID = [nsjsonserialization jsonobjectwithdata:_receivedata options:nsjsonreadingmutableleaves Error:&error] ; if (error) { NSLog (@ "Recivedata error reason: '%@ '.", [error localizeddescription]); } else { _datasource = [objid[@ "items"] mutablecopy]; [_tableview reloaddata];} } Data stream Receive Method-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{ //Accept Data [_ Receivedata Appenddata:data];}
iOS nsurlconnection network request data/get and POST method