1 //2 //ZFVIEWCONTROLLER.M3 //0628-Forms validation4 //5 //Created by Zfan on 14-6-28.6 //Copyright (c) 2014 Zfan. All rights reserved.7 //8 9 #import "ZFViewController.h"Ten #import "mbprogresshud+mj.h" One A @interfaceZfviewcontroller () - -@property (Weak, nonatomic) Iboutlet Uitextfield *Usernametextfield; the@property (Weak, nonatomic) Iboutlet Uitextfield *Passwordtextfield; - - @end - + @implementationZfviewcontroller - +- (void) Viewdidload A { at [Super Viewdidload]; - } - --(ibaction) Login: (ID) Sender - { - //Verify that the account and password are legal in if(Self.userNameTextField.text.length = =0) - { to[Mbprogresshud ShowError:@"Please enter your account number"]; + return; - } the * if(Self.passWordTextField.text.length = =0) $ {Panax Notoginseng[Mbprogresshud ShowError:@"Please enter your password"]; - return; the } + A //Remove account and password theNSString *username =Self.userNameTextField.text; +NSString *password =Self.passWordTextField.text; - $ //Generate URL $NSString *urlpath = [NSString stringWithFormat:@"http://192.168.1.101:8080/zfserver/login?username=%@&pwd=%@", userName, password]; -Nsurl *url =[Nsurl Urlwithstring:urlpath]; - the //Build Connection -Nsurlrequest *urlrequest =[Nsurlrequest Requestwithurl:url];Wuyi the //establish a connection and receive return data (synchronous execution) - //NSData *logindata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:nil Error:nil]; Wu - About //establish a connection and receive return data (asynchronous execution) $Nsoperationqueue *queue =[[Nsoperationqueue alloc] init]; -[Nsurlconnection sendasynchronousrequest:urlrequest queue:queue completionhandler:^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) { -NSData *logindata =data; -NSLog (@"%ld", (unsignedLong) logindata.length); A }]; + the //print the returned data length -NSLog (@"%@", self.userNameTextField.text); $NSLog (@"%@", self.passWordTextField.text); the //NSLog (@ "%ld", (unsigned long) logindata.length); the } the the @end
Key points of knowledge:
1> the full URL path by stitching the string through the NSString class method;
2> through the Nsurlrequest class method, using the URL to establish a nsurlrequest connection instance;
3> establishes the connection and accepts the return data through the Nsconnection class method, which can be implemented synchronously and asynchronously:
3.1> Sync mode
[Nsurlconnection sendsynchronousrequest:urlrequest Returningresponse:nil Error:nil];
This method does not create a new thread, the return value is NSData, and because the thread is not new, the thread will not continue to run down until the method is completed, causing the card to die easily;
3.2> Async Mode
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];[ Nsurlconnection sendasynchronousrequest:urlrequest queue:queue Completionhandler:^ (NSURLResponse *response, NSData *data, Nserror **logindata = data; NSLog (@ "%ld"long) logindata.length);}];
This method creates a new thread to accept the return data from the network side, the return value is void, and the statement in block is executed when the data is accepted;