iphone tableview&网络服务

时间:2010-04-11 21:08:40

标签: iphone-sdk-3.0 web-services uitableview

我有一个tableview,当用户根据选择的行选择一个我调用webservice的行时。

我的问题是我可以连接到webservice但我没有从webservice得到任何响应。我使用soap客户端来测试webservice是否正常工作。

//rootviewcontroller.m

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {....
    //call to webservice
    [self connectToWebService];
    }

在调试时,我发现我的代码没有使用以下任何方法

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error{}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {}

任何建议我哪里出错了??? 感谢

-(void)connectToWebService
{
        NSString *soapMsg = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<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/\">"
                         "<soap:Body>"
                             " <GetCount xmlns=\"http://192.168.1.104/Service1\">"
                             "<PropId>718</PropId>"
                             "</GetCount>"
                             "</soap:Body>"
                             "</soap:Envelope>"];
        NSURL *url = [NSURL URLWithString: 
                      @"http://192.168.1.104/defpath/service1.asmx"];
        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
        //---set the headers---
        NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
        [req addValue:@"text/xml; charset=utf-8" 
        forHTTPHeaderField:@"Content-Type"];
        [req addValue:@"http://192.168.1.104/defpath/Service1/GetCount" 
        forHTTPHeaderField:@"SOAPAction"];
        [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

        //---set the HTTP method and body---
        [req setHTTPMethod:@"POST"];
        [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

        conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
        if (conn) {
            webData = [[NSMutableData data] retain];
            }   

1 个答案:

答案 0 :(得分:0)

我不知道您的问题是否已解决: 你可以得到这样的答案:

-(void) connection:(NSURLConnection *) connection //Recive response
didReceiveResponse:(NSURLResponse *) response {
    [webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection //Repeative call method and append data to webData
    didReceiveData:(NSData *) data {
    [webData appendData:data];
}

-(void) connection:(NSURLConnection *) connection//If error occure error should be displayed  
  didFailWithError:(NSError *) error {
    NSLog(@"Problème de connexion au service web appelé");
    [webData release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSLog(@"OK. Bytes reçues: %d", [webData length]);

    NSString *theXML = [[NSString alloc] 
                        initWithBytes: [webData mutableBytes] 
                        length:[webData length] 
                        encoding:NSUTF8StringEncoding];
    //---shows the XML---
    NSLog(theXML);


    [theXML release];
}