2013-01-11 5 views
4

안녕하세요. 저는 웹 서비스를 전달하는 iphone 응용 프로그램을 작성 중이므로 두 텍스트 필드가 있습니다. 프로그램은 첫 번째 텍스트 필드의 값을 읽고 웹 서비스를 게시 한 다음 두 번째 쓰기가 필요한 웹 서비스에서 응답해야합니다. 텍스트 필드. 그러나 두 번째 텍스트 필드에서는 어떤 것도 쓰지 않습니다. 그것은 콘솔에 null을 출력합니다. 왜 그런가? 감사합니다. .iphone 웹 서비스 응용 프로그램

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    { 
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
     if (self) { 
      nodeContent = [[NSMutableString alloc]init]; 
     } 
     return self; 
    } 

- (IBAction)login:(id)sender { 

    NSLog(@"PASSWORD text: %@",password.text); 

    if ([password.text length]==0) { 

     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"WebService" message:@"Supply Data in text field" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok",nil]; 
     [alert show]; 
     [alert release]; 
    } 
    else { 

     NSString *soapFormat = [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" 
           "<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n" 
           "<Celsius>%@</Celsius>\n" 
           "</CelsiusToFahrenheit>\n" 
           "</soap:Body>\n" 
           "</soap:Envelope>\n",password.text]; 


     NSLog(@"The request format is %@",soapFormat); 

     NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"]; 

     NSLog(@"web url = %@",locationOfWebService); 

     NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService]; 

     NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]]; 

     [theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; 
     [theRequest addValue:@"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"]; 
     [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
     [theRequest setHTTPMethod:@"POST"]; 
     //the below encoding is used to send data over the net 
     [theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]]; 


     NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 

     if (connect) { 
      webData = [[NSMutableData alloc]init]; 
      startActivityIndicator; 
     } 
     else { 
      NSLog(@"No Connection established"); 
     } 


     // [self performSegueWithIdentifier:@"logindevam" sender:self]; 

    } 

} 
- (IBAction)sendkeyboard:(id)sender { 
    [sender 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); 

    xmlParser = [[NSXMLParser alloc]initWithData:webData]; 
    [xmlParser setDelegate: self]; 
    //[xmlParser setShouldResolveExternalEntities: YES]; 
    [xmlParser parse]; 
    // 
    [connection release]; 
    //[webData release]; 
    //[resultTable reloadData]; 
    stopActivityIndicator; 
} 


//xml delegates 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
{ 

} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    [nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if ([elementName isEqualToString:@"CelsiusToFahrenheitResult"]) { 
     NSLog(@"nodeContent: %@",nodeContent); // it becomes null 
     finaldata = nodeContent; 
     NSLog(@"finaldata: %@",finaldata);   // it becomes null 
     NSLog(@"username.text: %@",username.text); // it becomes null 
     username.text = finaldata; 

    } 
    username.text = finaldata; 
} 
+0

더 많은 세부 정보를 제공해야합니다. 효과가 있고 효과가없는 것은 무엇입니까? 웹 서비스가 호출 되었습니까? 돌아 왔습니까? 예상 된 데이터를 반환합니까? 어떤 코드가'null '을 콘솔에 출력합니까? 우리가 당신을 도울 수 있도록 도와주세요. – rmaddy

+0

[link]에서이 코드를 사용해 보았습니다. http://stackoverflow.com/questions/982622/consume-wcf-web-service-using-objective-c-on-iphone – lowcosthighperformance

+0

응? 그게 내 질문에 어떻게 대답 하나? 이미 코드를 올렸습니다. 이제 다른 코드로 연결됩니다. 죄송합니다. 코드가 실제로 얼마나 멀리 떨어져 있고 어디에 문제가 있는지에 대한 세부 정보를 제공 할 수 없다면 아무도 많은 도움을 드릴 수 없습니다. – rmaddy

답변

1

코드 :

-(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 = [theXML stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"]; 
    theXML = [theXML stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"]; 
    theXML = [theXML stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; 

    NSString *str, *result; 
    result = [[[NSString alloc] initWithFormat:@""] autorelease]; 

    NSArray *array=[theXML componentsSeparatedByString:@"<CelsiusToFahrenheitResult>"]; 
    for(int i=1;i<[array count];i++) 
    { 
     str=[array objectAtIndex:i]; 

     NSRange ranfrom=[str rangeOfString:@"</CelsiusToFahrenheitResult>"]; 
     result =[str substringToIndex:ranfrom.location]; 
    } 

    NSLog(@"\n -> %@", result); 

    t2.text = result; // Your Second textfield 

    [connection release]; 
} 

그것은 확실히 일 것입니다. 감사합니다. .

+0

XMLParser 클래스가 필요하지 않습니다. –

+1

고맙습니다. 작동합니다. – lowcosthighperformance

관련 문제