yassassin
yassassin

Reputation: 3195

Parse JSON message to retrieve Twitter data on iOS 5

I'm trying to display in my app some public tweets retrieved from Obama twitter profile. To retrieve twitter data, I've implemented this getTweets method:

-(void)getTweets
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:@"Obamabarak" forKey:@"screen_name"];
    [params setObject:@"10" forKey:@"count"];
    [params setObject:@"1" forKey:@"include_entities"];
    [params setObject:@"1" forKey:@"include_rts"];


    NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json"];

    TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
    {
        if (error != nil)
        {
            //  Inspect the contents of error 
            exit(-1);
        }
        else
        {
            [self fetchJSONData:responseData];
        }
    }
}

At this point, I've tried to implement the fetchJSONData method as follows:

- (void)fetchJSONData:(NSData *)responseData
{
NSError* error;

NSDictionary* jsonResults = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSArray *myArrayOfDictionaries = [[jsonResults objectForKey:@"tweets"] objectForKey:@"results"];

for (NSDictionary *myDictionary in myArrayOfDictionaries)
{
    // Get title of the image
    NSString *title = [myDictionary objectForKey:@"title"];
    ...

But it doesn't work and no record is displayed. I'm not sure if this is the right way and I don't know how to proceed. Can you please help me to find a way to display Obama tweets?

Thanks in advance

Upvotes: 0

Views: 2215

Answers (2)

user1541029
user1541029

Reputation:

Try this

#define jsonQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define jsonURL [NSURL URLWithString: @"Your LInk"]

@synthesize YOUR NSDICTIONARY;

- (void)issueLoadRequest
{
    // Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
    dispatch_async(jsonQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: jsonURL];
        [self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
    });
}

- (void)receiveData:(NSData *)data {
    // When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
    // going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
    self.YOUR NSDICTIONARY= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    [self.myTableView reloadData];
}

Upvotes: 1

yassassin
yassassin

Reputation: 3195

I finally get rid of this issue:

if ([urlResponse statusCode] == 200)
{
    // Parse the responseData, which we asked to be in JSON format for this request
    NSError *jsonParsingError = nil;
    //this is an array of dictionaries
    arrayTweets = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];

    //point to the first tweet
    NSDictionary *aTweet = [arrayTweets objectAtIndex:0];

    //write to log
    NSLog(@"text: %@", [aTweet objectForKey:@"text"]);
    NSLog(@"created_at: %@", [aTweet objectForKey:@"created_at"]);
}

I didn't find any good example on the internet, but this worked for me!!!

yassa

Upvotes: 1

Related Questions