Connection: didReceiveData: the request is successful and starts to receive data. If the data volume is large, it will be called multiple times;
Connection: didFailWithError: An exception occurred when loading data;
ConnectionDidFinishLoading: The data is successfully loaded and executed after the connection: didReceiveData method;
The MasterViewController. h code for the master View Controller Using asynchronous requests is as follows:
[Cpp]? # Import <UIKit/UIKit. h>
# Import "NSString + URLEncoding. h"
# Import "NSNumber + Message. h"
@ Interface MasterViewController: UITableViewController <NSURLConnectionDelegate>
@ Property (strong, nonatomic) DetailViewController * detailViewController;
// Save the data list
@ Property (nonatomic, strong) NSMutableArray * listData;
// Receives the data returned from the slave server.
@ Property (strong, nonatomic) NSMutableData * datas;
// Reload the table View
-(Void) reloadView :( NSDictionary *) res;
// Start requesting Web Service
-(Void) startRequest;
@ End
# Import <UIKit/UIKit. h>
# Import "NSString + URLEncoding. h"
# Import "NSNumber + Message. h"
@ Interface MasterViewController: UITableViewController <NSURLConnectionDelegate>
@ Property (strong, nonatomic) DetailViewController * detailViewController;
// Save the data list
@ Property (nonatomic, strong) NSMutableArray * listData;
// Receives the data returned from the slave server.
@ Property (strong, nonatomic) NSMutableData * datas;
// Reload the table View
-(Void) reloadView :( NSDictionary *) res;
// Start requesting Web Service
-(Void) startRequest;
@ End
The above code implements the NSURLConnectionDelegate protocol in the MasterViewController definition. The datas attribute is used to store the data returned from the server and is defined as a variable type. It is used to continuously append data to this datas during the loading of data from the server. The MasterViewController. m code is as follows:
[Cpp]/*
* Start to request Web Service
*/
-(Void) startRequest {
NSString * strURL = [[NSString alloc] initWithFormat:
@ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =% @",
@ "<Your iosbook1.com user email>", @ "JSON", @ "query"];
NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];
NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection * connection = [NSURLConnection alloc]
InitWithRequest: request
Delegate: self];
If (connection ){
_ Datas = [NSMutableData new];
}
}
# Pragma mark-NSURLConnection callback Method
-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {①
[_ Datas appendData: data];
}
-(Void) connection :( NSURLConnection *) connection didFailWithError: (NSError *) error {
NSLog (@ "% @", [error localizedDescription]);
}
-(Void) connectionDidFinishLoading: (NSURLConnection *) connection {②
NSLog (@ "request completed ...");
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: _ datas
Options: NSJSONReadingAllowFragments error: nil];
[Self reloadView: dict];
}
/*
* Start to request Web Service
*/
-(Void) startRequest {
NSString * strURL = [[NSString alloc] initWithFormat:
@ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =% @",
@ "<Your iosbook1.com user email>", @ "JSON", @ "query"];
NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];
NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection * connection = [NSURLConnection alloc]
InitWithRequest: request
Delegate: self];
If (connection ){
_ Datas = [NSMutableData new];
}
}
# Pragma mark-NSURLConnection callback Method
-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {①
[_ Datas appendData: data];
}
-(Void) connection :( NSURLConnection *) connection didFailWithError: (NSError *) error {
NSLog (@ "% @", [error localizedDescription]);
}
-(Void) connectionDidFinishLoading: (NSURLConnection *) connection {②
NSLog (@ "request completed ...");
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: _ datas
Options: NSJSONReadingAllowFragments error: nil];
[Self reloadView: dict];
}
In the connection: didReceiveData: Method of row ①, it is very important to understand this point by continuously receiving data from the server through the [_ datas appendData: data] statement. If the load is successful, the connectionDidFinishLoading of row ② will be called back. This method also means the end of the request. At this time, the data in _ datas is complete, send the data back to the View Controller of the presentation layer.