我正在尝试在我的 iphone 应用程序中使用 Web 服务来实现和解析 xml 文档:
这是我的请求(HTTP GET):
http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e
回应将是:
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
我的 testxml.h :
#import <UIKit/UIKit.h>
@interface testxmlViewController : UIViewController {
IBOutlet UILabel *lbl1;
IBOutlet UILabel *lbl2;
IBOutlet UIButton *submit;
NSMutableData *categoryData;
NSURLConnection *URLConnection;
NSMutableString *currnetCategory;
BOOL *hello;
}
@property (nonatomic, retain) NSMutableData *categoryData;
@property (nonatomic, retain) NSURLConnection *URLConnection;
@property (nonatomic, retain) NSMutableString *currnetCategory;
- (IBAction) submitClick :(id) sender;
@end
我的 testxml.m 文件:
#import "testxmlViewController.h"
@implementation testxmlViewController
@synthesize categoryData, URLConnection;
@synthesize currnetCategory;
- (IBAction) submitClick :(id) sender
{
lbl1.text = @"Submit button was clicked.";
static NSString *URLString = @"http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e";
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
URLConnection = [[[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] autorelease];
NSAssert(self.URLConnection != nil, @"Failure to create URL connection.");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.categoryData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[categoryData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"ERROR with theConenction");
[connection release];
[categoryData release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *theXML = [[NSString alloc] initWithBytes: [categoryData mutableBytes] length:[categoryData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:categoryData];
if( parser )
{
[parser release];
}
parser = [[NSXMLParser alloc] initWithData: categoryData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
[connection release];
[categoryData release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(elementName);
if ([elementName isEqualToString:@"category"])
{
lbl1.text = [attributeDict objectForKey:@"name"];
NSLog([attributeDict objectForKey:@"name"]);
//currnetCategory = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currnetCategory)
{
[currnetCategory appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(elementName);
}
我在我的 **GDB 中正确打印了所有值:**
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
2010-03-11 10:05:57.817 testxml[16203:207] categories
2010-03-11 10:05:57.819 testxml[16203:207] category
2010-03-11 10:05:57.822 testxml[16203:207] Trade Balance
2010-03-11 10:05:57.823 testxml[16203:207] category
2010-03-11 10:05:57.823 testxml[16203:207] categories
但它在执行didEndElement方法后终止了应用程序。
请你帮我解决这个问题。
谢谢。