iphone sdk xcode程序中如何调用web service? 现在我有一个MyEclipse 6.0 java写的web service,不知道该怎么在我的iphone程序中调用。还请高手指点!最好有代码示例。谢谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以在详细点吗? 初学iphone 不是很明白 请问楼主,这个问题解决了吗,我也遇到同样的问题,可以帮忙吗?谢谢了~~我想用soap,但是刚刚接触iphone不是很了解! 有两种方法生成SOAP本地代理类1:使用WSDL2OBJC.exe,生成SOAP代理类,引入工程中。2:访问http://sudzc.com/,在线提交服务地址或者上传WSDL文件,返回SOAP代理类,引入工程中。 #import <UIKit/UIKit.h>@interface Hello_SOAPViewController : UIViewController { IBOutlet UITextField *nameInput; IBOutlet UILabel *greeting; NSMutableData *webData; NSMutableString *soapResults; NSXMLParser *xmlParser; BOOL *recordResults;}@property(nonatomic, retain) IBOutlet UITextField *nameInput;@property(nonatomic, retain) IBOutlet UILabel *greeting;@property(nonatomic, retain) NSMutableData *webData;@property(nonatomic, retain) NSMutableString *soapResults;@property(nonatomic, retain) NSXMLParser *xmlParser;-(IBAction)buttonClick: (id) sender;@end#import "Hello_SOAPViewController.h"@implementation Hello_SOAPViewController@synthesize greeting, nameInput, webData, soapResults, xmlParser;-(IBAction)buttonClick:(id)sender{ recordResults = FALSE; 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" "<Hello xmlns=\"http://viium.com/\">\n" "<name>%@</name>\n" "</Hello>\n" "</soap:Body>\n" "</soap:Envelope>\n", nameInput.text ]; NSLog(soapMessage); NSURL *url = [NSURL URLWithString:@"http://viium.com/WebService/HelloWorld.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://viium.com/Hello" forHTTPHeaderField:@"SOAPAction"]; [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if( theConnection ) { webData = [[NSMutableData data] retain]; } else { NSLog(@"theConnection is NULL"); } [nameInput resignFirstResponder]; }-(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{ NSLog(@"ERROR with theConenction"); [connection release]; [webData release];}-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"DONE. Received Bytes: %d", [webData length]); NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; NSLog(theXML); [theXML release]; if( xmlParser ) { [xmlParser release]; } xmlParser = [[NSXMLParser alloc] initWithData: webData]; [xmlParser setDelegate: self]; [xmlParser setShouldResolveExternalEntities: YES]; [xmlParser parse]; [connection release]; [webData release];}-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qNameattributes: (NSDictionary *)attributeDict{ if( [elementName isEqualToString:@"HelloResult"]) { if(!soapResults) { soapResults = [[NSMutableString alloc] init]; } recordResults = TRUE; }}-(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 isEqualToString:@"HelloResult"]) { recordResults = FALSE; greeting.text = soapResults; [soapResults release]; soapResults = nil; }}/* Implement loadView if you want to create a view hierarchy programmatically- (void)loadView {} *//* Implement viewDidLoad if you need to do additional setup after loading the view.- (void)viewDidLoad { [super viewDidLoad];} */- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait);}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview // Release anything that's not essential, such as cached data}- (void)dealloc { [xmlParser release]; [super dealloc];}@end范例....... 其实从服务器上获取数据,除了使用webservice以外,也可以向服务器发送http请求,让服务器端返回一个比较好处理的格式,如JSON,并且在iphone os下有专门的处理 Json格式的类库,通过该类库我们可以很容易的将获取的json格式序列化成objective-c下的NSArray or NSDictionary 对象。 调用webservice 需要我们组装soap请求信息,维护不方便。iphone os也有比较优秀的封装好的处理http请求的类库ASIHttpRequest,通过该类库可以很easy的实现与服务器的交互。 能不能给我一些范例??我初学IPHONE 都不知应该怎样开始 同求示例源码!lz弄好的话麻烦发一份给我,先谢了![email protected] 我也想要一份 实例代码,楼主谢谢啦~~哪位大哥有的话麻烦发一份呀~[email protected] 不直接用系统的http类即可么,有这么复杂吗 新人请教一个关于Objective-c的语法问题 那位大深帮忙解决一下iphong编程时出错了,这是哪儿出错了怎么解决 啊 请问可以在 view 和webview之间转换吗 苹果虚拟机老是卡死? MAC下unicode编码 UITableViewCell复用重叠的问题!! ios 从一个界面传多个文本框的值给另一个界面 IOS蓝牙4.0开发数据接收问题 请教关于网络上传数据的问题 菜鸟求教,如何让ScrollView填满整个屏幕? 真机调试 初始化报错的问题请教 ios5 sqlite怎样设置串行模式?
1:使用WSDL2OBJC.exe,生成SOAP代理类,引入工程中。
2:访问http://sudzc.com/,在线提交服务地址或者上传WSDL文件,返回SOAP代理类,引入工程中。
#import <UIKit/UIKit.h>@interface Hello_SOAPViewController : UIViewController
{
IBOutlet UITextField *nameInput;
IBOutlet UILabel *greeting;
NSMutableData *webData;
NSMutableString *soapResults;
NSXMLParser *xmlParser;
BOOL *recordResults;
}@property(nonatomic, retain) IBOutlet UITextField *nameInput;
@property(nonatomic, retain) IBOutlet UILabel *greeting;
@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableString *soapResults;
@property(nonatomic, retain) NSXMLParser *xmlParser;-(IBAction)buttonClick: (id) sender;@end#import "Hello_SOAPViewController.h"@implementation Hello_SOAPViewController@synthesize greeting, nameInput, webData, soapResults, xmlParser;-(IBAction)buttonClick:(id)sender
{
recordResults = FALSE;
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"
"<Hello xmlns=\"http://viium.com/\">\n"
"<name>%@</name>\n"
"</Hello>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", nameInput.text
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://viium.com/WebService/HelloWorld.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://viium.com/Hello" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
[nameInput resignFirstResponder];
}-(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
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
[connection release];
[webData release];
}-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
if( [elementName isEqualToString:@"HelloResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = TRUE;
}
}
-(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 isEqualToString:@"HelloResult"])
{
recordResults = FALSE;
greeting.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
/*
Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
*//*
Implement viewDidLoad if you need to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc
{
[xmlParser release];
[super dealloc];
}
@end范例.......
iphone os也有比较优秀的封装好的处理http请求的类库ASIHttpRequest,通过该类库可以很easy的实现与服务器的交互。
能不能给我一些范例??我初学IPHONE 都不知应该怎样开始