iOS 使用ASIHTTPRequest 手机归属地查询

            使用ASI框架,必须先导入ASIHTTPRequest源文件和ASI依赖的框架,具体的怎么使用ASI可以google一下。

           这里我使用的查询接口

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=%@&userID=%@,

         新建一个xib,添加一个UITextField 和 UIButton,UITextField用于输入手机号码,如下图:

iOS 使用ASIHTTPRequest 手机归属地查询

绑定UIButton的点击事件-(IBAction)searchButtonClick:(id)sender;

//responds to the click event of the search button
-(IBAction)searchButtonClick:(id)sender
{
    
    [_textField resignFirstResponder];
    
    NSString * number = _textField.text;
    
    if([number length] == 0)
    {
        [CUtils showAlertWithTitle:nil withMessage:@"EmptyNumber"];
        return ;
    }
    
    NSString *urlString = [NSString stringWithFormat:@"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=%@&userID=%@",number,@""];
    
    ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];

    [request setFailedBlock:^{
        CCLog(@"Search Failed.");
       
    }];
    
    [request setCompletionBlock:^{
        [self hideNetworkActivity];
        CCLog(@"%@",[[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding]);

        [self performSelectorOnMainThread:@selector(parserXML:) withObject:[request responseData] waitUntilDone:YES];
    }];
    
    [request startAsynchronous];
    
}
-(void)parserXML:(NSData *)data
{
    
    CXMLParser * parser = [[CXMLParser alloc] initWithData:data completionHandler:^(NSString * value){
        
        CCLog(@"Parser completion handler");
        if(value.length == 0)
        {
            
            return ;
        }
        
        CCLog(@"%@",value);
        
     
    
    }];
    


    [parser release];
    
}
      CXMLParser 类是我自定义一个用于解析xml的类,下面是改类的实现代码:

     CXMLParser.h文件:

#import <Foundation/Foundation.h>
typedef void (^XMLParserCompletionHandler)(NSString * value);
@interface CXMLParser : NSObject <NSXMLParserDelegate>
@property (nonatomic,retain) NSString * currentElement;
@property (nonatomic,retain) NSXMLParser * xmlParser;
@property (nonatomic,retain) NSMutableString * result;

@property (nonatomic,copy) XMLParserCompletionHandler completionHandler;

-(id)initWithData:(NSData *)data completionHandler:(XMLParserCompletionHandler)paramHandler;
@end
      CXMLParser.m文件:

#import "CXMLParser.h"

@implementation CXMLParser
@synthesize completionHandler;
-(id)initWithData:(NSData *)data completionHandler:(XMLParserCompletionHandler)paramHandler
{
    self = [self init];
    if(self)
    {
        self.completionHandler = paramHandler;
        _result = [[NSMutableString alloc] init];
        _xmlParser = [[NSXMLParser alloc] initWithData:data];
        _xmlParser.delegate = self;
        [_xmlParser parse];
    }
    return self;
}

-(id) init
{
    self = [super init];
    if(self)
    {
        
    }
    return  self;
}



-(void)dealloc
{
    [_xmlParser release];
    _xmlParser = nil;
    [_currentElement release];
    _currentElement = nil;
    self.completionHandler = NULL;
    [_result release];
    _result = nil;
    [super dealloc];
}

#pragma mark - NSXMLParserDelegate Methods
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    NSLog(@"Parser start");
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{

    if(self.completionHandler)
        self.completionHandler(self.result);
    else
        NSLog(@"The completion handler is nil");
    NSLog(@"Parser end");
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    _currentElement = [elementName retain];
    NSLog(@"%@",_currentElement);
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"%@",string);

    if([_currentElement isEqualToString:@"string"])
    {
        [_result appendString:string];
    }
    else
    {
        NSLog(@"Element name : %@ is error",_currentElement);
    }
}

-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"parser error : %@",parseError);
    if(self.completionHandler)
        self.completionHandler(@"");
}

@end


你可能感兴趣的:(ios,Objective-C,ASIHTTPRequest,NSXMLParser,手机归属地查询)