2013-03-13 62 views
1

我写的iPad应用程序需要连接到返回互联网服务和整数这样的:连接从IOS到ASP.Net Webservce

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">0</int> 

我的代码看起来像这样

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:verficationURL]]; 
    [request setHTTPMethod:@"GET"]; 

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 


} 

-(void) connection: (NSURLConnection *)connection didReceiveData:(NSData *)data{ 

    [self.responseData appendData:data]; 



} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 


    NSString *result = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; 


    NSLog(@"array: %s", result); 

} 

连接完成加载,但我无法从结果中获得值0。什么是正确的方式来做到这一点。谢谢

+0

哪个解析器用于配对XML? – Girish 2013-03-13 07:05:04

+0

@Girish正如你所看到的,我还没有尝试过使用XML解析器。在这种情况下,我们是否需要使用XML解析器? – unni 2013-03-13 07:43:45

回答

0

我假设你得到的结果,所以只会回应解析。

我通常使用XMLReader进行简单的xml解析。还有其他第三方库可以使用,或直接使用NSXMLParsing,但是,您需要为此编写更多的代码。

使用它,解析的代码应该是这样的:

NSError *error = nil; 
NSDictionary *dict = [XMLReader dictionaryForXMLData:self.responseData error:&error]; 
NSNumber *result = [dict valueForKey:@"int"]; 
if(error||!result){ 
    // handle error (either the result is not xml format, or the xml doesn't contain a 'int' key) 
} 
else { 
    // handle result 
} 

我没有测试过这个确切的代码,但它应该是正确的大部分。

0

请创建基于REST的Web服务,因为它变得非常容易与数据库进行交互并转到JSON。