为什么我在.verifyCredentials()上收到错误

时间:2016-08-16 19:18:20

标签: java android twitter

我想使用twitter的Fabric api获取数据,但每当我倾向于验证凭据并使用回调时,它会显示错误,具体而言,"参数的长度不同"

void getUserData() {
        Twitter.getApiClient(session).getAccountService()
                .verifyCredentials(true, false, new Callback<User>() {

                    @Override
                    public void failure(TwitterException e) {

                    }

                    @Override
                    public void success(Result<User> userResult) {

                        User user = userResult.data;
                        String twitterImage = user.profileImageUrl;

                        try {
                            Log.d("imageurl", user.profileImageUrl);
                            Log.d("name", user.name);
                            Log.d("email",user.email);
                            Log.d("des", user.description);
                            Log.d("followers ", String.valueOf(user.followersCount));
                            Log.d("createdAt", user.createdAt);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }

                });


    }

2 个答案:

答案 0 :(得分:1)

只需更改Build.Gradle中的twitter依赖项即可 从

compile('com.twitter.sdk.android:twitter:2.0.0@aar') {
    transitive = true;
}

compile('com.twitter.sdk.android:twitter:1.11.0@aar') {
    transitive = true;
}

新版本的.verifyCredentials()方法不接受回调,因此您的错误。

答案 1 :(得分:1)

如果您查看结构文档,它会显示该方法的两个版本,但是当我尝试在Android Studio中打开源代码但它只有没有回调的版本时。

您可以按如下方式解决问题:

//Getting the account service of the user logged in
Call<User> call = Twitter.getApiClient(session).getAccountService()
        .verifyCredentials(true, false);
call.enqueue(new Callback<User>() {
        @Override
        public void failure(TwitterException e) {
            //If any error occurs handle it here
        }
        @Override
        public void success(Result<User> userResult) {
            //If it succeeds creating a User object from userResult.data
            User user = userResult.data;
                    String twitterImage = user.profileImageUrl;

                    try {
                        Log.d("imageurl", user.profileImageUrl);
                        Log.d("name", user.name);
                        Log.d("email",user.email);
                        Log.d("des", user.description);
                        Log.d("followers ", String.valueOf(user.followersCount));
                        Log.d("createdAt", user.createdAt);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
        }
    });

Source

Documentation