如何使用Twitter Fabric Android获取关注者列表?

我想在不使用twitter4j的情况下使用Twitter Rest Api。 Fabric很好但我找不到像gettingUserFollowers()这样的方法。 我不知道为什么会这样。 无论如何,我想使用此服务呼叫我的用户关注者ID。 https://dev.twitter.com/rest/reference/get/followers/ids

我看过面料网站的教程( http://docs.fabric.io/android/twitter/access-rest-api.html#tweets )。 有一个用于获取自定义服务的类。 但我无法理解我如何称之为发送参数。我将其改为如下

import com.twitter.sdk.android.core.TwitterApiClient; import com.twitter.sdk.android.core.TwitterSession; import retrofit.http.GET; import retrofit.http.Query; public class MyTwitterApiClient extends TwitterApiClient { public MyTwitterApiClient(TwitterSession session) { super(session); } public CustomService getCustomService() { return getService(CustomService.class); } interface CustomService { @GET("/1.1/followers/ids.json") void show(@Query("user_id") long id); } } 

我想当我发送身份证时,服务会带来粉丝。

 MyTwitterApiClient aa = new MyTwitterApiClient(session); aa.getCustomService().show(userId); 

但应用程序停止了。我错了什么?

LogCat是

 5897-15897/com.tumymedia.tumer.lylafortwitter E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.tumymedia.tumer.lylafortwitter, PID: 15897 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=140, result=-1, data=Intent { (has extras) }} to activity {com.tumymedia.tumer.lylafortwitter/com.tumymedia.tumer.lylafortwitter.MainActivity}: java.lang.IllegalArgumentException: CustomService.show: Must have either a return type or Callback as last argument. at android.app.ActivityThread.deliverResults(ActivityThread.java:4058) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4101) at android.app.ActivityThread.access$1400(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1497) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) Caused by: java.lang.IllegalArgumentException: CustomService.show: Must have either a return type or Callback as last argument. at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:123) at retrofit.RestMethodInfo.parseResponseType(RestMethodInfo.java:285) at retrofit.RestMethodInfo.(RestMethodInfo.java:113) at retrofit.RestAdapter.getMethodInfo(RestAdapter.java:213) at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:236) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at com.tumymedia.tumer.lylafortwitter.$Proxy16.show(Unknown Source) at com.tumymedia.tumer.lylafortwitter.MainActivity$1.success(MainActivity.java:55) at com.twitter.sdk.android.core.identity.TwitterAuthClient$CallbackWrapper.success(TwitterAuthClient.java:230) at com.twitter.sdk.android.core.Callback.success(Callback.java:40) at com.twitter.sdk.android.core.identity.AuthHandler.handleOnActivityResult(AuthHandler.java:91) at com.twitter.sdk.android.core.identity.TwitterAuthClient.onActivityResult(TwitterAuthClient.java:161) at com.twitter.sdk.android.core.identity.TwitterLoginButton.onActivityResult(TwitterLoginButton.java:131) at com.tumymedia.tumer.lylafortwitter.MainActivity.onActivityResult(MainActivity.java:96) at android.app.Activity.dispatchActivityResult(Activity.java:6543) at android.app.ActivityThread.deliverResults(ActivityThread.java:4054)            at android.app.ActivityThread.handleSendResult(ActivityThread.java:4101)            at android.app.ActivityThread.access$1400(ActivityThread.java:177)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1497)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:145)            at android.app.ActivityThread.main(ActivityThread.java:5942)            at java.lang.reflect.Method.invoke(Native Method)            at java.lang.reflect.Method.invoke(Method.java:372)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

实际上,Fabric使用改进来进行REST Api调用,并且如Fabric文档中所述,为了获取关注者的ID,我们需要将user_id作为参数传递并在响应中检索列表。

MyTwitterApiClient.java

 import com.twitter.sdk.android.core.Callback; import com.twitter.sdk.android.core.TwitterApiClient; import com.twitter.sdk.android.core.TwitterSession; import retrofit.client.Response; import retrofit.http.GET; import retrofit.http.Query; public class MyTwitterApiClient extends TwitterApiClient { public MyTwitterApiClient(TwitterSession session) { super(session); } /** * Provide CustomService with defined endpoints */ public CustomService getCustomService() { return getService(CustomService.class); } } // example users/show service endpoint interface CustomService { @GET("/1.1/followers/ids.json") void list(@Query("user_id") long id, Callback cb); } 

现在,在MainActivity中,我们将对用户进行身份validation,然后通过获取会话,我们将检索与userid相对应的所有关注者的列表。

MainActivity.java

 public class MainActivity extends AppCompatActivity { // Note: Your consumer key and secret should be obfuscated in your source code before shipping. private static final String TWITTER_KEY = "YOUR_TWITTER_KEY"; private static final String TWITTER_SECRET = "YOUR_TWITTER_SECRET"; TwitterLoginButton loginButton; SharedPreferences shared; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.with(this, new Twitter(authConfig), new Crashlytics()); setContentView(R.layout.activity_main); shared = getSharedPreferences("demotwitter", Context.MODE_PRIVATE); loginButton = (TwitterLoginButton) findViewById(R.id.login_button); loginButton.setCallback(new Callback() { @Override public void success(Result result) { // Do something with result, which provides a TwitterSession for making API calls TwitterSession session = Twitter.getSessionManager() .getActiveSession(); TwitterAuthToken authToken = session.getAuthToken(); String token = authToken.token; String secret = authToken.secret; //Here we get all the details of user's twitter account System.out.println(result.data.getUserName() + result.data.getUserId()); Twitter.getApiClient(session).getAccountService() .verifyCredentials(true, false, new Callback() { @Override public void success(Result userResult) { User user = userResult.data; //Here we get image url which can be used to set as image wherever required. System.out.println(user.profileImageUrl+" "+user.email+""+user.followersCount); } @Override public void failure(TwitterException e) { } }); shared.edit().putString("tweetToken", token).commit(); shared.edit().putString("tweetSecret", secret).commit(); 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(result.toString()); Log.d("Result", result.toString()); Toast.makeText(getApplicationContext(), result.data, Toast.LENGTH_LONG).show(); } @Override public void failure(TwitterException exception) { // Do something on failure System.out.println(exception.getMessage()); } }); MyTwitterApiClient apiclients=new MyTwitterApiClient(session); apiclients.getCustomService().list(result.data.getUserId(), new Callback() { @Override public void failure(TwitterException arg0) { // TODO Auto-generated method stub } @Override public void success(Result arg0) { // TODO Auto-generated method stub BufferedReader reader = null; StringBuilder sb = new StringBuilder(); try { reader = new BufferedReader(new InputStreamReader(arg0.response.getBody().in())); String line; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } String result = sb.toString(); System.out.println("Response is>>>>>>>>>"+result); try { JSONObject obj=new JSONObject(result); JSONArray ids=obj.getJSONArray("ids"); //This is where we get ids of followers for(int i=0;i 

在Retrofit 2.0中,界面 – >

 public interface FollowersService { @GET("/1.1/followers/list.json") Call list(@Query("screen_name") String userId); } 

调用方法 – >

 FollowersService followersService = followersTwitterApiClient.getFollowersService(); Call call = followersService.list(userId); call.enqueue(followerCallback); 

自定义Api客户端 – >

 public class FollowersTwitterApiClient extends TwitterApiClient { public FollowersTwitterApiClient(TwitterSession twitterSession){ super(twitterSession); } public FollowersService getFollowersService(){ return getService(FollowersService.class); } } 

您需要对Twitter进行身份validation才能调用API并获得结果。 我没有在这段代码中看到这种情况。 (也许你是在这段代码之外做的?)

有关Twitter身份validation的详细信息,请访问https://dev.twitter.com/oauth