2011-12-28 16 views
0

Obama twitter profileから取得した公開ツイートをいくつか表示しようとしています。 Twitterのデータを取得するには、私はこのgetTweetsメソッド実装しました。この時点でiOS 5でTwitterデータを取得するためのJSONメッセージを解析する

-(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]; 
     } 
    } 
} 

を、私は次のようにfetchJSONDataメソッドを実装しようとしました:

- (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"]; 
    ... 

をしかし、それは動作しませんし、レコードは表示されません。これが正しい方法であるかどうかわからないし、どうやって進めるのか分からない。あなたはオバマのつぶやきを表示する方法を見つけるのを助けてくれますか?事前

+0

こんにちは、任意の提案を試してみてください?私はまだこれに固執している! :( – yassassin

答えて

1

おかげで、私が最終的にこの問題を取り除く:

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"]); 
} 

私は、インターネット上の任意の良い例を見つけることができませんでしたが、これは私のために働きました!

yassa

1

が問題でこの

#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]; 
} 
関連する問題