获取Twitter粉丝列表

时间:2013-02-18 10:56:45

标签: android twitter twitter4j

我正在开发Android应用以获取Twitter粉丝。以下代码用于身份验证,我已成功登录。

consumer = new CommonsHttpOAuthConsumer(
                        key,
                        secret);
                CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
                        "http://twitter.com/oauth/request_token",
                        "http://twitter.com/oauth/access_token",
                        "http://twitter.com/oauth/authorize");

                String authUrl = provider.retrieveRequestToken(consumer,
                        TWITTER_CALLBACK_URL);
                Log.e("Twittwer ", "Please authorize this app!");

                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(authUrl)));

为了获得关注者列表,我使用了下面的代码,但是我收到错误并且附加了logcat

AccessToken accessToken = new AccessToken(consumer.getToken(),
                    consumer.getTokenSecret());
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey(key);
            builder.setOAuthConsumerSecret(secret);
            Configuration conf = builder.build();
            twitter = new TwitterFactory(conf).getInstance();
            twitter.setOAuthAccessToken(accessToken);
            IDs followersId = twitter.getFollowersIDs(twitter.getId());
            do {
                for (long i : followersId.getIDs()) {



                    System.out.println("follower ID #" + i);
                    System.out.println(twitter.showUser(i).getName());
                    System.out.println(twitter.showUser(i)
                            .getProfileImageURL());
                    System.out.println(twitter.showUser(i).getURL());

                }
            } while (followersId.hasNext());
        } catch (TwitterException e) {
            System.out.println("e........"+e.getMessage());
            e.printStackTrace();

        }

logcat的:

02-18 16:13:10.632: W/System.err(734): Received authentication challenge is null 02-18 16:13:10.682: W/System.err(734): Relevant discussions can be found on the Internet at: 02-18 16:13:10.682: W/System.err(734):    http://www.google.co.jp/search?q=a1259409 or 02-18 16:13:10.702: W/System.err(734):     http://www.google.co.jp/search?q=06b97274 02-18 16:13:10.712: W/System.err(734): TwitterException{exceptionCode=[a1259409-06b97274 2e9324ac-d074d7d2], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3} 02-18 16:13:10.752: W/System.err(734):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:192) 02-18 16:13:10.903: W/System.err(734):   at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61) 02-18 16:13:11.012: W/System.err(734):  at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:89) 02-18 16:13:11.062: W/System.err(734):  at twitter4j.TwitterBaseImpl.fillInIDAndScreenName(TwitterBaseImpl.java:126) 02-18 16:13:11.123: W/System.err(734):     at twitter4j.TwitterBaseImpl.getId(TwitterBaseImpl.java:118) 02-18 16:13:11.142: W/System.err(734):     at com.nest.twitterfriendsfinder.MainActivity$getFollowers.doInBackground(MainActivity.java:276) 02-18 16:13:11.152: W/System.err(734):     at com.nest.twitterfriendsfinder.MainActivity$getFollowers.doInBackground(MainActivity.java:1) 02-18 16:13:11.192: W/System.err(734):   at android.os.AsyncTask$2.call(AsyncTask.java:264) 02-18 16:13:11.212: W/System.err(734):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 02-18 16:13:11.232: W/System.err(734):    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 02-18 16:13:11.302: W/System.err(734):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 02-18 16:13:11.362: W/System.err(734):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 02-18 16:13:11.362: W/System.err(734):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 02-18 16:13:11.362: W/System.err(734):   at java.lang.Thread.run(Thread.java:856) 02-18 16:13:11.362: W/System.err(734): Caused by: java.io.IOException: Received authentication challenge is null 02-18 16:13:11.372: W/System.err(734):    at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:397) 02-18 16:13:11.372: W/System.err(734):  at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:345) 02-18 16:13:11.372: W/System.err(734):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:276) 02-18 16:13:11.372: W/System.err(734):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479) 02-18 16:13:11.372: W/System.err(734):    at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:34) 02-18 16:13:11.382: W/System.err(734):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:156)

这段代码有什么问题。我在一些帖子中读到了检查模拟器中的时间戳。但是模拟器中的时间和日期是正确的。请给我一些帮助。谢谢提前..

[编辑] 我this对吗?

1 个答案:

答案 0 :(得分:0)

在我的项目中,我有这个代码来获取关注者。 也许主要观点与使用AsyncTask

有关
    if (twitter == null){
        ConfigurationBuilder builder = new ConfigurationBuilder();
        Configuration conf = builder
        .setOAuthConsumerKey(CONSUMER_KEY)
        .setOAuthConsumerSecret(CONSUMER_SECRET)
        .setOAuthAccessToken(oAuthAccessToken)
        .setOAuthAccessTokenSecret(oAuthAccessTokenSecret)
        .build();
        twitter = new TwitterFactory(conf).getInstance();
    }


    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            long lCursor = -1;
            AccessToken accessToken = new AccessToken(oAuthAccessToken, oAuthAccessTokenSecret);
            long userId = accessToken.getUserId();
            try {
                IDs followersIDs = twitter.getFollowersIDs(userId, lCursor);
                do {
                    for (long i : friendsIDs.getIDs()){
                        Log.d("follower ID #", String.valueOf(i));
                    }
                }while(followersIDs.hasNext());
            } catch (TwitterException e) {
                Log.i("fetchFollowers", e.getErrorMessage());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void param) {
        }

    }.execute();