iOS nsurlconnection network request data/get and POST method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.