在ios开发中,总结下和web数据的交换的一点经验,主要用webservers,http post ,get
1,iphone条用webservers接口——有soap,http post ,http get,三种方法
1)soap——自己构建一个xml的请求,这个比较烦麻烦
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>\n"
"<getLossDogInfomationOrderByLossTimer xmlns=\"http://www.027aigou.com/\">\n"
"</getLossDogInfomationOrderByLossTimer>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",@"5"];
NSURL *url = [NSURL URLWithString:@"http://www.027aigou.com/DesktopModules/webservice/webservice.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.027aigou.com/getLossDogInfomationOrderByLossTimer" forHTTPHeaderField:@"SOAPAction"];
[ theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[ theRequest setHTTPMethod:@"POST"];
[ theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//NSLog(@"*******************请求哦2!");
//请求
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
//NSLog(@"*******************请求哦3!");
//如果连接已经建好,则初始化data
if( theConnection )
{
webData = [[NSMutableData data] retain];
//NSLog(@"*******************请求哦!");
}
else
{
IsFinish = YES;
//NSLog(@"theConnection is NULL");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
//如果电脑没有连接网络,则出现此信息(不是网络服务器不通)
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"连接服务器失败,请稍后再试!"
delegate:self
cancelButtonTitle:@"好的,我知道了" otherButtonTitles:nil];
[alert show];
[alert release];
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//得到一个xml结果
//获取的内容不能为空
if (webData!=nil) {
[connection release];
//重新加載xmlParser
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
}
else
{
return;
}
[webData release];
}
缺点:这是一个异步请求,不能即使的得到结果,决定下一步执行的操作。不利于用于注册和登陆
2,同步请求:
同步是指当客户端调用post/get的方式的函数向服务器发出数据请求后,该函数不会直接返回,只有得到服务器响应或者请求时间timeout之后才会返回继续执行其它任务——用于及时的相应用户操作
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>\n"
"<getLossDogInfomationOrderByLossTimer xmlns=\"http://www.027aigou.com/\">\n"
"</getLossDogInfomationOrderByLossTimer>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",@"5"];
NSURL *url = [NSURL URLWithString:@"http://www.027aigou.com/DesktopModules/webservice/webservice.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.027aigou.com/getLossDogInfomationOrderByLossTimer" forHTTPHeaderField:@"SOAPAction"];
[ theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[ theRequest setHTTPMethod:@"POST"];
[ theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//NSLog(@"*******************请求哦2!");
NSURLResponse *respone;
NSError *error;
NSData*myReturn=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theRequest error:error];
NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]);