以JSON方式获取Twitter公共提要 [英] Fetching twitter public feeds as JSON
问题描述
作为我的项目的一部分,我需要显示iOS应用程序中的推文列表.我尝试使用以下代码,但返回了JSON.相同的代码适用于版本1.现在twitter api版本为1.1,并再次警告我TWRequest is deprecated
.即使我使用SLRequest
遇到了同样的事情,这种弃用也不是问题.在这里,我的问题是我需要在没有身份验证或有身份验证的情况下获取特定用户tweets的JSON
As a part of my project I need to show the list of tweets in iOS app. I tried with the below code but its returning the JSON. The same code is works fine with version 1. Now the twitter api version is 1.1 and one another warning I got that TWRequest is deprecated
. This deprecation is not the issue of this even I got same thing with SLRequest
. Here my problem is I need to fetch the JSON of Particular user tweets without authentication or with authentication
TWRequest *postequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.0/statuses/user_timeline.json?screen_name=coolCracker&count=2"] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode]==200) {
NSError *err;
NSDictionary *PublicTimeline=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];
output=[NSString stringWithFormat:@"HTTP response status: %li\nPublic timeline:\n%@",(long)[urlResponse statusCode],PublicTimeline];
}
else
{
output=[NSString stringWithFormat:@"No feed HTTP response was: %li\n",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayResults:) withObject:output waitUntilDone:NO];
}];
-(void)displayResults:(NSString *)text
{
NSLog(@"tweets feed %@",text);
}
推荐答案
我在工作中使用了它,并且效果很好.使用STTwitter.在此处下载STTwitter文件.
I used this in my work and it worked pretty well. Use STTwitter. Download STTwitter files here.
将其放置在视图中didload:
Place this in your view didload:
STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"your consumer key"
consumerSecret:@"your secret key"];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {
[twitter getUserTimelineWithScreenName:@"your twitter account name"
successBlock:^(NSArray *statuses) {
self.twitterFeedList = [NSMutableArray arrayWithArray:statuses];
[self->listView reloadData];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
这篇关于以JSON方式获取Twitter公共提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!