Twitter 如何在 Android Studio 中使用 Fabric 插件获取用户个人资料图片

Posted

技术标签:

【中文标题】Twitter 如何在 Android Studio 中使用 Fabric 插件获取用户个人资料图片【英文标题】:Twitter How to get user profile pic using Fabric plugin in Android Studio 【发布时间】:2015-02-11 13:26:44 【问题描述】:

登录后我在 android Studio 中使用 Fabric 插件,我得到了会话、用户名和用户 ID。还得到了电子邮件地址。但找不到任何获取用户个人资料图片的选项。

  /

/inti twitter client
        loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
        loginButton.setCallback(new Callback<TwitterSession>() 
            @Override
            public void success(Result<TwitterSession> result) 
                // Do something with result, which provides a TwitterSession for making API calls
                System.out.println("twitter success"+result.data.getUserId()+result.data.getUserName());
                gettwitteremail();
            

            @Override
            public void failure(TwitterException exception) 
                // Do something on failure
            
        );

private void gettwitteremail()
TwitterSession session =
        Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
TwitterAuthClient authClient = new TwitterAuthClient();
authClient.requestEmail(session, new Callback() 
    @Override
    public void success(Result result) 
        // Do something with the result, which provides
        // the email address
        System.out.println("twitter sucess"+result.data);
    

    @Override
    public void failure(TwitterException exception) 
        // Do something on failure
        System.out.println("twitter sucess"+exception.getMessage());
        exception.printStackTrace();
    
);

已修复

使用以下代码修复

TwitterSession session =
                Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
TwitterApiClient twitterApiClient =  Twitter.getApiClient();
StatusesService twapiclient = twitterApiClient.getStatusesService();
twapiclient.userTimeline(twitteruserid,null,null,null,null,null,null,null,null,new Callback<List<Tweet>>() 
    @Override
    public void success(Result<List<Tweet>> listResult) 

        System.out.println("listResult"+listResult.data.size());
        System.out.println("listResult"+listResult.data.get(0).user);
        System.out.println("listResult"+listResult.data.get(0).user.profileImageUrl);
        userInfo.imageurl = listResult.data.get(0).user.profileImageUrl;
        
    

    @Override
    public void failure(TwitterException e) 

    
);

【问题讨论】:

我修复了,借助 there rest api 【参考方案1】:

尝试使用此代码获取用户个人资料图片。

 public void getTwitterData(final TwitterSession session) 
       MyTwitterApiClient tapiclient = new MyTwitterApiClient(session);
       tapiclient.getCustomService().show(session.getUserId(),
       new Callback<User>() 
           @Override
           public void success(Result<User> result) 

              TwitterAuthToken authToken = session.getAuthToken();
              String token = authToken.token;
              String secret = authToken.secret;
              name.setText(result.data.name);
              location.setText(result.data.location);
              new ImageDownloader(profileImageView)
              .execute(result.data.profileImageUrl);

              Log.d("Name", name);
              Log.d("city", location);

              

              public void failure(TwitterException exception) 
              // Do something on failure
              exception.printStackTrace();
             
        );

    ****************
         class MyTwitterApiClient extends TwitterApiClient 
            public MyTwitterApiClient(TwitterSession session) 
                super(session);
            

            public CustomService getCustomService() 
                return getService(CustomService.class);
            


        

        interface CustomService 
            @GET("/1.1/users/show.json")
            void show(@Query("user_id") long id, Callback<User> cb);
        

************
    class ImageDownloader extends AsyncTask<String, Void, Bitmap> 
        ImageView bmImage;

        public ImageDownloader(ImageView bmImage) 
            this.bmImage = bmImage;
        

        protected Bitmap doInBackground(String... urls) 
            String url = urls[0];
            Bitmap mIcon = null;
            try 
                InputStream in = new java.net.URL(url).openStream();
                mIcon = BitmapFactory.decodeStream(in);
             catch (Exception e) 
                Log.e("Error", e.getMessage());
            
            return mIcon;
        

        protected void onPostExecute(Bitmap result) 
            bmImage.setImageBitmap(result);
        
    

【讨论】:

以上是关于Twitter 如何在 Android Studio 中使用 Fabric 插件获取用户个人资料图片的主要内容,如果未能解决你的问题,请参考以下文章

如何在按钮点击 android - twitter4j 上获取 twitter 热门话题?

从结构中删除后,如何将 twitter 集成到 android 中以进行 twitter 登录?

如何学习Android adb命令?

如何在 android 本机页面中显示 Twitter 视频

android:如何从 twitter 获取趋势?

Android,如何获取 Facebook/Twitter/etc 联系人照片