Afnetworking Form-Network request in the form of-nsurlconnection

Source: Internet
Author: User

One: nsurlconnection network Request

Nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; // Async Methods [Nsurlconnection sendasynchronousrequest:urlrequest queue:queue completionhandler:^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) {        *str = [[NSString alloc] initwithdata:data encoding: Nsutf8stringencoding];        NSLog (@ "%@", str);}];

Two:afnetworking Form network request

  1. Method One

  [Afhttprequestoperationmanager manager]; Manager.requestSerializer.timeoutInterval  = 30  ;manager.responseserializer  = [ Afjsonresponseserializer serializer];manager.responseserializer.acceptablecontenttypes  = [NSSet Setwithobject:@ " "]; //  //  Httpbody  //  Nsdictionary *httpbodyparameters = @{@" http ": @" Body "};  
//kjsonsourceurladdress request address[Manager get:kjsonsourceurladdress_1 Parameters:nil success:^ (Afhttprequestoperation *operation,IDresponseobject) {//nsdictionary *jsondict = (nsdictionary *) Responseobject;//[self displaywithparseddic:jsondict];} failure:^ (Afhttprequestoperation *operation, Nserror *error) {Uialertview*alert = [[Uialertview alloc] Initwithtitle:@"Error"Message:[nsstring stringWithFormat:@"%@", ERROR]Delegate: Nil Cancelbuttontitle:@"Close"Otherbuttontitles:nil, nil]; [Alert show];};

2. Method Two

Nsurl *url =[Nsurl Urlwithstring:kjsonsourceurladdress_1]; Nsurlrequest*urlrequest = [Nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreturncachedataelseload TimeoutInterval: -];//nsmutableurlrequest *test = [[Afhttprequestserializer serializer] multipartformrequestwithmethod:@ "POST" Urlstring:kjsonsourceurladdress_1 Parameters:nil constructingbodywithblock:^ (id<afmultipartformdata> FormData) {//        //TODO File Upload////[FormData appendpartwithfiledata:<# (NSData *) #>Name:<# (NSString *) #>FileName:<# (NSString *) #>MimeType:<# (NSString *) #>]//    }];//    //Network GET request//nsmutableurlrequest *requestget = [Nsmutableurlrequest requestwithurl:url];//Requestget.httpmethod = @ "GET";//requestget.timeoutinterval = +;////    //Network POST request//nsmutableurlrequest *requestpost = [Nsmutableurlrequest requestwithurl:url];//Requestpost.httpmethod = @ "POST";//requestpost.timeoutinterval = +;//NSData *httpbody = [Kjsonsourceurladdress_1 datausingencoding:nsutf8stringencoding];//NSLog (@ "%@", httpbody);//requestpost.httpbody = httpbody;afhttprequestoperation*operation =[[Afhttprequestoperation alloc] Initwithrequest:urlrequest];operation.respons Eserializer=[Afjsonresponseserializer serializer];//Many background formatting problems are resolved with this line, not necessarilyOperation.responseSerializer.acceptableContentTypes = [Nsset setwithobject:@"text/html"]; [Operation Setcompletionblockwithsuccess:^ (Afhttprequestoperation *operation,IDresponseobject) {//NSLog (@ "%@", responseobject);Nsdictionary *jsondict = (Nsdictionary *) Responseobject;        [Self displaywithparseddic:jsondict]; } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {Uialertview*alert = [[Uialertview alloc] Initwithtitle:@"Error"Message:[nsstring stringWithFormat:@"%@", ERROR]Delegate: Nil Cancelbuttontitle:@"Close"Otherbuttontitles:nil, nil]; [Alert show];};//Upload Progress//[Operation setuploadprogressblock:^ (Nsuinteger Byteswritten, Long long totalbyteswritten, long long totalbytesexp Ectedtowrite) {////    }];[[Nsoperationqueue Mainqueue] addoperation:operation];

Added: JSON parsing of the system's own

// JSON data parsed into Nsdictionary Nsdictionary *parsedata = [nsjsonserialization jsonobjectwithdata:_jsondata options:nsjsonreadingmutableleaves Error :&ERROR]; // nsdictionary Convert to JSON () NSData *jsondata = [nsjsonserialization datawithjsonobject:dict options:nsjsonwritingprettyprinted Error:nil];

Afnetworking Form-Network request in the form of-nsurlconnection

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.