IOS _ network request_proxy Method
# Pragma mark-network request proxy method (asynchronous)-(IBAction) DelegateButtonDidClicked :( UIButton *) sender {// 1. Splice urlString. The URL must contain http: // NSString * urlString = @ http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx? Date = 20131129 & startRecord = 1 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 213; // 2. Create a URL Based on the string (Uniform Resource Locator) NSURL * url = [NSURL URLWithString: urlString]; // 3. Create a request object NSURLRequest * request = [NSURLRequest requestWithURL: url] based on the url. // 4. Request data (proxy callback method) follows the proxy
[NSURLConnection connectionWithRequest: request delegate: self];}
Proxy Method
The client receives a response from the server.
# Pragma mark | the client receives the response from the server-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {// only execute once // initialize the receiveData object self. receiveData = [NSMutableData data];}
Data received by the client (not all at once)
# Pragma mark | data received by the client (not all obtained at a time)-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {// new data is returned each time. // concatenate data (requesting a bit of stitching) [self. receiveData appendData: data];}
The client has received the data.
# Pragma mark | the client has received the data-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// parse the data NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: self. receiveData options :( NSJSONReadingMutableContainers) error: nil]; NSArray * array = dict [@ news]; self. newsArray = [NSMutableArray array]; for (NSDictionary * smallDict in array) {NewsModal * modal = [[NewsModal alloc] init]; [modal convert: smallDict]; [self. newsArray addObject: modal];} for (NewsModal * modal in self. newsArray) {NSLog (% @, modal. title );}}
Network request failed
# Pragma mark | Network request failed-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (, error );}