0

在我的应用程序中,我使用以下方式从 twitter 句柄获取状态:http://api.twitter.com/1/statuses/user_timeline/ABCD.xml 但有时它在我的表格视图中没有显示任何内容。我正在使用 NSXMLParser。这是代码:

Class A : 
 xmlParser = [[XMLParser alloc] loadXMLByURL:@"http://api.twitter.com/1/statuses/user_timeline/ABCD.xml"];

XMLParser.m


    -(id) loadXMLByURL:(NSString *)urlString
    {
        //spinnerControl = [[SpinnerController alloc]init];
        //[spinnerControl getAlertViewWithSpinner];
        tweets          = [[NSMutableArray alloc] init];
        NSURL *url      = [NSURL URLWithString:urlString];
        NSData *data    = [[NSData alloc] initWithContentsOfURL:url];
        parser          = [[NSXMLParser alloc] initWithData:data];
        NSString * result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Complete:%@",result);
        parser.delegate = self;
        [parser parse];
        return self;
    }



    -(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        if ([elementname isEqualToString:@"status"])
        {
            currentTweet = [Tweet alloc];
        }
    }

    -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {

            currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    }


    -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        if ([elementname isEqualToString:@"text"])
        {
            currentTweet.content = currentNodeContent;
        }
        if (alreadyHaveDate != 1)
        {
            if ([elementname isEqualToString:@"created_at"])
            {
            currentTweet.dateCreated = currentNodeContent;
                alreadyHaveDate = 1;
            }
        }
        if ([elementname isEqualToString:@"profile_image_url"]) {

            currentTweet.artistImage = currentNodeContent;
        }
        if ([elementname isEqualToString:@"name"]) {
            currentTweet.artistName = currentNodeContent;
        }
        if ([elementname isEqualToString:@"screen_name"]) {
            currentTweet.screenName = currentNodeContent;
        }
        if ([elementname isEqualToString:@"status"])
        {
            [tweets addObject:currentTweet];
            alreadyHaveDate = 0;
        }
        //[spinnerControl  removeAlertView];
    }

Tweet.h :
@interface Tweet : NSObject
{
    NSString     *content;
    NSString     *dateCreated;
    NSString     *artistImage;
    NSString     *artistName;
    NSString     *screenName;
}

@property (nonatomic, retain) NSString   *content;
@property (nonatomic, retain) NSString   *dateCreated;
@property (nonatomic, retain) NSString   *artistImage;
@property (nonatomic, retain) NSString   *artistName; 
@property (nonatomic, retain) NSString     *screenName;
@end

Tweet.m

@implementation Tweet


@synthesize content, dateCreated,artistImage,artistName,screenName;


@end

特别是在设备使用 3G 的情况下。

4

1 回答 1

0

我没有看到您的 parse 函数代码,但是您怎么知道您实际上是从 API 获取一些数据?

您说这尤其是在设备使用 3G 的情况下,因此您可能希望在尝试解析之前验证您可以访问 API 并且您没有空数据。

于 2012-04-13T20:07:38.107 回答