1

TWRequest によって返された JSON をループして、ツイートの地理情報を取得するにはどうすればよいですか? 以下のコードを使用しています。不明な部分をマークアップしました。テキストコンポーネントは正常に動作しますが、地理データの配列を作成してこれにアクセスする方法がわかりません...

    - (void)fetchTweets
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];


    //NSLog(@"phrase carried over is %@", delegate.a);

    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=%@", delegate.a]] 
                                             parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if ([urlResponse statusCode] == 200) 
         {
             // The response from Twitter is in JSON format
             // Move the response into a dictionary and print
             NSError *error;        
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
             //NSLog(@"Twitter response: %@", dict);

             NSArray *results = [dict objectForKey:@"results"];


             //Loop through the results


             for (NSDictionary *tweet in results) {
                 // Get the tweet
                 NSString *twittext = [tweet objectForKey:@"text"];
                 //added this one - need to check id NSString is ok??
                 NSString *twitlocation = [tweet objectForKey:@"geo"];

                 // Save the tweet to the twitterText array
                 [_twitterText addObject:twittext];

                 //this is the loop for the location
                [twitterLocation addObject:twitlocation];
             }

             dispatch_async(dispatch_get_main_queue(), ^{

[self.tableView reloadData];

             });




         }

         else
             NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
     }];



}
4

1 に答える 1

1

「geo」は非推奨であり、おそらくまったく入力されていません。私が覚えている限りでは、Twitter API v1.0 でも廃止されました。このコードを試してください:

- (void)fetchTweets
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

    //NSLog(@"phrase carried over is %@", delegate.a);

    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=%@", delegate.a]]
                                             parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if ([urlResponse statusCode] == 200)
         {
             // The response from Twitter is in JSON format
             // Move the response into a dictionary and print
             NSError *error;
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
             //NSLog(@"Twitter response: %@", dict);

             NSArray *results = [dict objectForKey:@"results"];


             //Loop through the results


             for (NSDictionary *tweet in results) {
                 // Get the tweet
                 NSString *twittext = [tweet objectForKey:@"text"];
                 //added this one - need to check id NSString is ok??
                 id jsonResult = [tweet valueForKeyPath:@"coordinates.coordinates"];
                 if ([NSNull null] != jsonResult) {
                     if (2 == [jsonResult count]) {
                         NSDecimalNumber* longitude = [jsonResult objectAtIndex:0];
                         NSDecimalNumber* latitude = [jsonResult objectAtIndex:1];
                         if (longitude && latitude) {
                             // here you have your coordinates do whatever you like
                             [twitterLocation addObject:[NSString stringWithFormat:@"%@,%@", latitude, longitude]];
                         }
                         else {
                             NSLog(@"Warning: bad coordinates: %@", jsonResult);
                         }
                     }
                     else {
                         NSLog(@"Warning: bad coordinates: %@", jsonResult);
                     }
                 }

                 // Save the tweet to the twitterText array
                 [_twitterText addObject:twittext];

             }

             dispatch_async(dispatch_get_main_queue(), ^{
                 [self.tableView reloadData];
             });
         }
         else
             NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
     }];
}
于 2012-12-14T19:53:57.900 に答える