json形式で得られたtwitterデータの分析
3つのprojectファイルのデータ取得→表示部分を比較する。
「Nemotter」自分のタイムラインデータをxml形式で取得、パースしtable viewに表示することが出来る
「Tweeting」自分のタイムラインデータをjson形式で取得、そのままtext windowに表示することが出来る
「TwitterAPIExample」自分のタイムラインデータをjson形式で取得、一番新しいつぶやきのみパースしてtext boxに表示することが出来る
「Tweeting」のデータ取得実装部
- (IBAction)getPublicTimeLine:(id)sender {
// Create a request which grabs the public timeline.
// This example uses version 1 of the Twitter API.
// This may need to be changed to whichever version is currently appropriate.
TWRequest *postRequest = [[TWRequest alloc] initWithURL:
[NSURL URLWithString: @"http://api.twitter.com/1/statuses/public_timeline.json"] parameters:nil requestMethod:TWRequestMethodGET];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode] == 200) {
// Parse the responseData, which we asked to be in JSON format for this request, into an NSDictionary using NSJSONSerialization.
NSError *jsonParsingError = nil;
NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
output = [NSString stringWithFormat:@"HTTP response status: %i\nPublic timeline:\n%@", [urlResponse statusCode], publicTimeline];
}
else {
output = [NSString stringWithFormat:@"HTTP response status: %i\n", [urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
}];
}
「TwitterAPIExample」のデータ取得実装部
-(void)getTweet {
// Specify the URL and parameters
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/user_timeline/nemotz.json"];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"count", nil];
// Create the TweetRequest object
TWRequest *tweetRequest = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET];
[tweetRequest performRequestWithHandler: ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// Request completed and we have data
// Output it!
NSError *jsonError = nil;
id timelineData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error: &jsonError];
if(timelineData == NULL) {
// There was an error changing the data to a Foundation Object,
// so we'll output a bunch of debug information.
NSString *myString =[[NSString alloc] initWithData: responseData encoding:NSUTF8StringEncoding];
NSLog(@"\n\nConversion to object failed");
NSLog(@"HTTP Response code:%d", [urlResponse statusCode]);
NSLog(@"Output from server:%@", myString);
// NSLog(@"JSON Error:%@\n\n", [jsonError localisedDescription]);
abort();
// TODO : Show a graceful error message here
}
NSDictionary *timelineDict = (NSDictionary*)timelineData;
NSLog(@"\n\nConversion ");
NSLog(@"%@\n\n", timelineDict);
self.tweetLabel.text = [[(NSArray*)timelineDict objectAtIndex:0] objectForKey:@"text"];
}];
}
※ twitter apiには認証を必要とするものとしないものがある。
不必要:publicタイムライン、userタイムライン
必要:homeタイムライン、friendsタイムライン
userとhomeの違いは不明。
最終更新:2011年12月02日 16:12