2010-04-11 57 views
0

我有一個tableview,當用戶選擇一行我調用webservice取決於選擇哪一行。iphone tableview&webservice

我的問題是我可以連接到web服務,但我沒有從web服務得到任何響應。我使用soap客戶端來測試web服務是否正常工作。

//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]; 
      } 
+0

的「connectToWebService」方法的內容可能是這裏的關鍵。我們可以看到該方法的內容嗎? – gcamp 2010-04-11 21:39:53

+0

讓我改寫這個問題...... 當用戶從tableview中選擇一行時,應該從Web服務連接調用哪種方法? 應該從didSelectRowAtIndexPath調用? – jsp 2010-04-14 17:28:17

回答

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]; 
}