iphone网络请求与响应,并对响应的xml进行处理

来源:互联网 发布:avsow最新域名 编辑:程序博客网 时间:2024/05/17 08:55

网络请示与响应,并对

      @interface

    NSMutableData *responseData;
    NSXMLParser *coutryParse;
    NSMutableArray *allCoutry;
    RootViewController *rootViewController;

 

@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic)    NSMutableArray *allCoutry;


- (void) getCoutry;

- (void) getCoutryFromWebService:(NSString*) URLStr;

- (void) parseCoutry : (NSData *) CoutryXMLData;

 

@implementation

 

#define Coutry_URL @"http://www.accuweather.com/includes/ajax-functions/favoriteCountries.asp?region=ASI"
#define ErrorGettingCoutry @"Error Getting Coutry"
#define GettingCoutry @"Getting Coutry ..."

 

- (void) getCoutry
{
    [self getCoutryFromWebService:Coutry_URL];
}

- (void) getCoutryFromWebService:(NSString*) URLStr
{
    [rootViewController setTitle:@"Getting Coutry ..."];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
   
    NSURL *currentURL = [NSURL URLWithString:URLStr];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:currentURL
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:10.0];
   
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        responseData = [[NSMutableData data] retain];
    }
    else
    {
        [rootViewController setTitle:ErrorGettingCoutry];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    }
   
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [rootViewController setTitle:ErrorGettingCoutry];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    [connection release];
    [responseData release];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse*)response;
    NSInteger statusCode = [HTTPResponse statusCode];
    if (statusCode == 404 || statusCode == 500)
    {
        [rootViewController setTitle: ErrorGettingCoutry];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
        [connection cancel];
    }
    else {
        [responseData setLength:0];
    }
}

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

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self parseCoutry:responseData];
    [connection release];
    [responseData release];
}

- (void) parseCoutry : (NSData *) CoutryXMLData
{
    if (coutryParse) {
        [coutryParse release];
    }
    coutryParse = [[NSXMLParser alloc] initWithData:CoutryXMLData ];
    coutryParse.delegate = self;
    [coutryParse setShouldResolveExternalEntities:NO];
    [coutryParse parse];
}

- (void)parser:(NSXMLParser *)parser
    didStartElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI
    qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"countries"]) {
        if(allCoutry)
            [allCoutry removeAllObjects];
        else {
            allCoutry = [[NSMutableArray alloc] init];
        }

    }
    if ([elementName isEqualToString:@"country"]) {

        //attributeDict当前节点属性

        [allCoutry addObject:attributeDict];
    }
}

//得到值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{                                     
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"country"]) {
        [rootViewController reloadData];
    }
}

原创粉丝点击