**************
#import "HMViewController.h"#import "mbprogresshud+mj.h"@interfaceHmviewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield*username, @property (weak, nonatomic) Iboutlet Uitextfield*pwd;-(ibaction) login;@end@implementationHmviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{[Self.view endediting:yes];}-(ibaction) Login {//1. User nameNSString *usernametext =Self.username.text; if(Usernametext.length = =0) {[Mbprogresshud showerror:@"Please enter user name"]; return; } //2. PasswordNSString *pwdtext =Self.pwd.text; if(Pwdtext.length = =0) {[Mbprogresshud showerror:@"Please enter your password"]; return; } //3. Send the user name and password to the server (take the HTTP protocol)//Create a URL: request pathNSString *urlstr = [NSString stringWithFormat:@"http://localhost:8080/mjserver/login?username=%@&pwd=%@", Usernametext, Pwdtext]; Nsurl*url =[Nsurl Urlwithstring:urlstr]; //Create a requestNsurlrequest *request =[Nsurlrequest Requestwithurl:url];//NSLog (@ "Begin---"); //Send an asynchronous request (send a request on the main thread)//Queue: Store Completionhandler this taskNsoperationqueue *queue =[Nsoperationqueue Mainqueue]; [Nsurlconnection sendasynchronousrequest: Request Queue:queue Completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) { //this block will be automatically called when the request is complete. if(Connectionerror | | data = =Nil) {[Mbprogresshud showerror:@"request failed"]; return; } //parse the JSON data returned by the serverNsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; NSString*error = dict[@"Error"]; if(Error) {//{"Error": "User name does not exist"}//{"Error": "Password is incorrect"}[Mbprogresshud Showerror:error]; } Else { //{"Success": "Login Succeeded"}NSString *success = dict[@"Success"]; [Mbprogresshud showsuccess:success]; } }]; //NSLog (@ "End---");}@end
iOS network next day-02-Asynchronous HTTP request block callback parsing