如何从webservice发送数据?目标c

时间:2011-09-17 10:41:46

标签: iphone objective-c web-services

我正在使用webservice。 以下是用于存储数据的Web服务的链接。

http://www.chandrakumar.com/iphone_soap_server.php

WSDL网址:http://www.chandrakumar.com/iphone_soap_server.php?wsdl

在此服务中调用insertdata方法需要4个参数:
名称
电话
电子邮件
得分
成功插入后,您将收到消息“用户信息已保存”

对于此事件我使用此代码。

 -(IBAction)Save:(id)sender{

NSString *name = txt_name.text;
NSString *phone = txt_phone.text;
NSString *email = txt_email.text;
NSString *score = txt_score.text;
NSString *urlpath;
urlpath = [@"http://www.chandrakumar.com/" stringByAppendingString:@"iphone_soap_server.php?name="];
urlpath = [urlpath stringByAppendingString:name];
urlpath = [urlpath stringByAppendingString:@"&phone="];
urlpath = [urlpath stringByAppendingString:phone];
urlpath = [urlpath stringByAppendingString:@"&email="];
urlpath = [urlpath stringByAppendingString:email];
urlpath = [urlpath stringByAppendingString:@"&score="];
urlpath = [urlpath stringByAppendingString:score];

NSURL *url=[[NSURL alloc] initWithString:[urlpath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *a = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setTimeoutInterval:60];
//NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
//  [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//  [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//  [req setHTTPMethod:@"POST"];
//  [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[[NSError alloc] init] autorelease];

NSData *responseData= [NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error];

//RMAppDelegate *apDelObj = (RMAppDelegate *)[UIApplication sharedApplication].delegate;
if(error.domain != nil)
{
    NSString *errorDesc=[[error userInfo] objectForKey:@"NSLocalizedDescription"];
    //apDelObj.globalError = errorDesc;
    NSLog(@"%@",errorDesc);
}
else {
    NSLog(@"nil error");
}

NSLog( @"%@",responseData);
}

但我没有收到回复消息“用户信息已保存”。这种方法有什么用呢?我想在服务器中存储四个参数。怎么样?

1 个答案:

答案 0 :(得分:0)

你必须在这里查看..

if(error.domain != nil)
{
    NSString *errorDesc=[[error userInfo] objectForKey:@"NSLocalizedDescription"];
    //apDelObj.globalError = errorDesc;
    NSLog(@"%@",errorDesc);
}
else {
    NSString* str = [NSString stringWithUTF8String:[responseData bytes]];
    NSLog(@"Result : %@", str);
}