今天作了個查詢QQ是否在線的,效果圖以下,web
http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx
網絡
輸入參數:QQ號碼 String,默認QQ號碼:8698053。返回數據:String,Y = 在線;N = 離線;E = QQ號碼錯誤;A = 商業用戶驗證失敗;V = 免費用戶超過數量app
查詢結果沒有設置爲在線等,仍是Y,這個用switch就能夠了,或者寫一個專門的類用來反饋結果
在xib中設置如上圖,textfiled的鍵盤設置爲數字鍵盤網站
聲明文件內容
atom
#import <UIKit/UIKit.h>url
@interface YUViewController : UIViewController<NSXMLParserDelegate,NSURLConnectionDelegate>//前者用來解析XML,後者用於網絡鏈接spa
@property (weak, nonatomic) IBOutlet UITextField *qq;.net
- (IBAction)doQuery:(id)sender;orm
@property (strong, nonatomic) NSMutableData *webData;xml
@property (strong, nonatomic) NSMutableString *soapResults;
@property (strong, nonatomic) NSXMLParser *xmlParser;
@property (nonatomic) BOOL elementFound;
@property (strong, nonatomic) NSString *matchingElement;
@property (strong, nonatomic) NSURLConnection *conn;
@end
//SOAP是簡單對象訪問協議,它可當作是HTTP與XML的結合,其中XML部分是做爲HTTP報文的實體主體部分。具體信息能夠參考百度
//在iOS中使用SOAP,須要咱們本身組裝XML格式的字符串,當XML字符串比較長的時候會變得很麻煩。另外,咱們在寫XML格式的字符串時也要常常使用轉義字符「\」。這個是服務提供方 www.webxml.com.cn
實現文件內容
//
// YUViewController.m
// atQQ
//
// Created by chen on 3/9/13.
// Copyright (c) 2013 IOSCHEN. All rights reserved.
//
#import "YUViewController.h"
@interface YUViewController ()
@end
@implementation YUViewController
@synthesize webData;
@synthesize soapResults;
@synthesize xmlParser;
@synthesize elementFound;
@synthesize matchingElement;
@synthesize conn;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 開始查詢
- (IBAction)doQuery:(id)sender
{
NSString *number =_qq.text;
// 設置咱們以後解析XML時用的關鍵字,與響應報文中Body標籤之間的qqCheckOnlineResult標籤對應
matchingElement = @"qqCheckOnlineResult";
// 建立SOAP消息,內容格式就是網站上提示的請求報文的實體主體部分
NSString *soapMsg = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap12:Envelope "
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
"xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"> "
"<soap12:Body>"
"<qqCheckOnline xmlns=\"http://WebXml.com.cn/\">"
"<qqCode>%@</qqCode>"
"</qqCheckOnline>"
"</soap12:Body>"
"</soap12:Envelope>", number];
// 將這個XML字符串打印出來
NSLog(@"%@", soapMsg);
// 建立URL,內容是前面的請求報文報文中第二行主機地址加上第一行URL字段
NSURL *url = [NSURL URLWithString: @"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx"];
// 根據上面的URL建立一個請求
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
// 添加請求的詳細信息,與請求報文前半部分的各字段對應
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
// 設置請求行方法爲POST,與請求報文第一行對應
[req setHTTPMethod:@"POST"];
// 將SOAP消息加到請求中
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
// 建立鏈接
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [NSMutableData data];
}
[_qq resignFirstResponder];
}
#pragma mark -
#pragma mark URL Connection Data Delegate Methods
// 剛開始接受響應時調用
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{
[webData setLength: 0];
}
// 每接收到一部分數據就追加到webData中
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data {
[webData appendData:data];
}
// 出現錯誤時
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error {
conn = nil;
webData = nil;
}
// 完成接收數據時調用
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
// 打印出獲得的XML
NSLog(@"%@", theXML);
// 使用NSXMLParser解析出咱們想要的結果
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
}
#pragma mark -
#pragma mark XML Parser Delegate Methods
// 開始解析一個元素名
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
if ([elementName isEqualToString:matchingElement]) {
if (!soapResults) {
soapResults = [[NSMutableString alloc] init];
}
elementFound = YES;
}
}
// 追加找到的元素值,一個元素值可能要分幾回追加
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
if (elementFound) {
[soapResults appendString: string];
}
}
// 結束解析這個元素名
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:matchingElement]) {
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"QQ是否在線"
message:[NSString stringWithFormat:@"%@", soapResults]
delegate:self
cancelButtonTitle:@"肯定"
otherButtonTitles:nil];
[alert show];
elementFound = FALSE;
// 強制放棄解析
[xmlParser abortParsing];
soapResults=nil;//防止查詢兩次的時候顯示了兩次的結果
}
}
// 解析整個文件結束後
- (void)parserDidEndDocument:(NSXMLParser *)parser {
if (soapResults) {
soapResults = nil;
}
}
// 出錯時,例如強制結束解析
- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
if (soapResults) {
soapResults = nil;
}
}
@end
參考博文 http://my.oschina.net/plumsoft/blog/75277