【问题标题】:iphone web service applicationiphone网络服务应用程序
【发布时间】:2013-01-11 00:07:49
【问题描述】:

您好,我正在编写与 Web 服务通信的 iphone 应用程序,所以我有两个文本字段,程序需要读取第一个文本字段的值并发布 Web 服务,然后在 Web 服务的响应之后它需要编写第二个文本字段。但是在第二个文本字段中它没有写任何东西。它在控制台上打印 null 。为什么会变成? 谢谢。

 - (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 打印到控制台?帮助我们帮助您。
  • 我在 [link]stackoverflow.com/questions/982622/… 中尝试了此代码
  • 嗯?这如何回答我的任何问题?您已经发布了代码。现在您正在链接到其他代码。抱歉,但如果您无法向我们提供有关您的代码实际执行的程度以及问题所在的任何详细信息,那么没有人可以为您提供太多帮助。
  • 能否也提供日志

标签: iphone ios web-services


【解决方案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];
}

它肯定会起作用。 谢谢。

【讨论】:

  • 不需要 XMLParser 类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 2021-01-17
  • 2015-11-19
相关资源
最近更新 更多