NSURLConnection, NSURLRequest, NSURL 을 간단하게 사용 합 니 다.

다음은 코드 이 고 주석 도 비교적 명확 하 게 썼 다.
헤더 파일 은 프로 토 콜 을 실현 해 야 합 니 다. NSURLConnectionDelegate 와 NSURLConnectionDataDelegate
//
//  HttpDemo.h
//  MyAddressBook
//
//  Created by hherima on 14-6-23.
//  Copyright (c) 2014 . All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface HttpDemo : NSObject<NSURLConnectionDelegate, NSURLConnectionDataDelegate>
{
    NSMutableData *receivedData;
    NSURLConnection *theConncetion;
}
@end

원본 파일
//
//  HttpDemo.m
//  MyAddressBook
//
//  Created by hherima on 14-6-23.
//  Copyright (c) 2014 . All rights reserved.
//

#import "HttpDemo.h"
@implementation HttpDemo
/*
 NSURLConnection             URL  ,                   ,       delegate               
 
   :
 1、     NSURL
 2、   NSURL  NSURLRequest,             
 3、  NSURLConnection  ,  NSURLRequest   delegate  
       ,    nil,           NSMutalbeData         
 */
- (id)init {
    self = [super init];
    // Override point for customization after application launch.
    NSURLRequest* theRequest = [NSURLRequest requestWithURL:
                                [NSURL URLWithString:@"http://www.baidu.com"]//
                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                            timeoutInterval:60.0];
    //   initWithRequest: delegate:    ,       ,   (delegate)
    //  connectionDidFinishLoading:  connection:didFailWithError:    
    //           cancel:       
    theConncetion=[[NSURLConnection alloc]
                   initWithRequest:theRequest delegate:self];
    if(theConncetion)
    {
        //  NSMutableData
        receivedData = [NSMutableData data];
    }
    else
    {
        //    ;
    }
    
    return self;
}


//               NSURLResponse      ,         connection:didReceiveResponse:  ,        NSURLResponse            ,mime  ,                

//【   】,               connection:didReceiveResponse:                   (    mime  ),                    ,         
-(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse*)response
{
    [receivedData setLength:0];
}


//        ,       ,       connection:didReceiveData:                  ,         
-(void) connection:(NSURLConnection*)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}


//         connection:didFailWithError   ,              
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    theConncetion = nil;
    
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    
}
//      ,  ,           ,     connectionDidFinishLoading:              ,       ,       
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    //do something with the data
    NSString *s = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"succeeded %@",s);
    theConncetion = nil;
    [receivedData setLength:0];
}
@end

좋은 웹페이지 즐겨찾기