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