iphone如何写代码去访问webservice?

Evan0712 2010-05-05 11:25:24
以前用Java的时候,采用Ksoap2去访问webservice, 请问大侠们,在iphone上面,应该如何去访问webservice呢?
Thanks!
...全文
1102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjnhzwy 2010-09-15
  • 打赏
  • 举报
回复
给你一个我做过的案例吧是关于一个webservice的解析的 关键市解析xml文件,在苹果底下没有现成的类将xml文件解析成树状的类,自己按照帮助文档的案例推敲吧!

#import "QQViewController.h"


@implementation QQViewController

@synthesize qqCodeText;
@synthesize qqStatusLabel;

@synthesize webData;
@synthesize soapResults;
@synthesize xmlParser;
@synthesize recordResults;
@synthesize activityIndicatorView;

//处理文字输入完毕键盘的隐藏 或者在输入完毕按回车时直接进行查询
-(IBAction)textDidEndExit:(id)sender{
//[qqCodeText resignFirstResponder];
[sender resignFirstResponder];
}

- (void)getQQStatus{
recordResults=NO;

//soap request message
NSString *soapMessage=[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>"
"<qqCheckOnline xmlns=\"http://WebXml.com.cn/\">"
"<qqCode>%@</qqCode>"
"</qqCheckOnline>"
"</soap:Body>"
"</soap:Envelope>",qqCodeText.text];
//请求地址
NSURL *url=[NSURL URLWithString:@"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx"];

NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];

NSString *msgLegth=[NSString stringWithFormat:@"%d",[soapMessage length]];

[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://WebXml.com.cn/qqCheckOnline" forHTTPHeaderField:@"SOAPAction"];


[theRequest addValue:msgLegth forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding ]];

//request
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

//connection
if(theConnection){
webData=[[NSMutableData data]retain];
}else{
NSLog(@"theConnection is NULL");
}

}

-(IBAction)selectStatus{
//qqStatusLabel.text=@"Getting time ...";

//等待界面
[activityIndicatorView startAnimating];

[qqCodeText resignFirstResponder];//为什么在这里释放

[self getQQStatus];
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

self.qqCodeText=nil;
self.qqStatusLabel=nil;

[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
[qqCodeText release];
[qqStatusLabel release];

[super dealloc];
}

//接受到数据
-(void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data{
[webData appendData:data];
NSLog(@"connection didReceiveData:2");
}

//没有接受到数据
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
/* This method is called when the server has determined that it has
enough information to create the NSURLResponse. It can be called
multiple times, for example in the case of a redirect, so each time
we reset the data. */

NSLog(@"webdata length is :%d",[webData length]);
[webData setLength:0];
NSLog(@"connection:didReceiveResponse:1");
}

//
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"ERROR with theConnection");
[connection release];
[webData release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *theXML=[[NSString alloc]
initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];

[theXML release];
if(xmlParser){
[xmlParser release];
}

xmlParser=[[NSXMLParser alloc] initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];

[connection release];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"qqCheckOnlineResult"]){
if(!soapResults){
soapResults=[[NSMutableString alloc] init];
}
recordResults=YES;
}
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(recordResults){
[soapResults appendString:string];
}
}

//在这里接收到返回的数据
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

if([elementName isEqual:@"qqCheckOnlineResult"]){
recordResults=FALSE;

/*处理结果Y = 在线;N = 离线;E = QQ号码错误;A = 商业用户验证失败;V = 免费用户超过数量*/

[activityIndicatorView stopAnimating];
if([soapResults isEqualToString:@"Y"]){
qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"在线"];
}else if([soapResults isEqualToString:@"N"]){
qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"离线"];
}else if([soapResults isEqualToString:@"E"]){
qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"QQ号码错误"];
}else if([soapResults isEqualToString:@"A"]){
qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"商业用户验证失败"];
}else if([soapResults isEqualToString:@"V"]){
qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"免费用户超过数量"];
}

[soapResults release];
soapResults=nil;
}
}

-(void)parserDidStartDocument:(NSXMLParser *)parser{
//解析开始
//[activityIndicatorView ];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
//解析完成
}
-(void)viewDidLoad{
[activityIndicatorView stopAnimating];
[activityIndicatorView hidesWhenStopped];

[super viewDidLoad];
}

@end


loveluru 2010-09-15
  • 打赏
  • 举报
回复
用的还是soap!
Kareny000 2010-06-29
  • 打赏
  • 举报
回复
请问,这个问题解决了吗?我遇到同样的问题,也不知道怎么解决?能帮帮忙吗~~谢谢了~~

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧