嗨我正在寫iphone應用程序,溝通web服務,所以我有兩個文本字段,程序需要讀取第一個文本字段的值併發布web服務然後在從web服務的響應後它需要寫第二個文本域。但在第二個文本框中,它沒有寫任何東西。它在控制檯上輸出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;
}
你需要提供更多的細節。什麼工作,什麼不工作? Web服務是否被調用?它會返回嗎?它是否會返回預期的數據?什麼代碼在控制檯打印'null'?幫助我們幫助你。 – rmaddy
我試過這段代碼[鏈接] http://stackoverflow.com/questions/982622/consume-wcf-web-service-using-objective-c-on-iphone – lowcosthighperformance
咦?這怎麼回答我的任何問題?您已經發布了您的代碼。現在你鏈接到其他代碼。對不起,如果你不能提供任何關於你的代碼實際得到多少以及問題出在哪裏的細節,沒有人能夠爲你提供很多幫助。 – rmaddy