无法使用 Fabric Android 获取 Twitter 中的关注者列表
Unable to get the list of followers in twitter using Fabric Android
我正在为 Twitter 使用 Fabric SDK。在这里,我可以按照其文档中的描述发出登录请求。现在我不想获取已登录用户的关注者列表并显示在 listview.I 中尝试了很多链接,但这些链接对我不起作用
然后我尝试了这段代码,但每次 运行 我的应用程序仍然崩溃。
class MyTwitterApiClient extends TwitterApiClient
{
public MyTwitterApiClient(TwitterSession session)
{
super(session);
}
public CustomService getCustomService() {
return getService(CustomService.class);
}
interface CustomService {
@GET("/1.1/followers/list.json")
Call<User> show(@Query("user_id") Long userId, @Query("screen_name") String
var, @Query("skip_status") Boolean var1, @Query("include_user_entities") Boolean var2, @Query("count") Integer var3, Callback < Followers > cb);
}
}
这样调用:
new MyTwitterApiClient(session).getCustomService().show(userID, null, true, true, 100, new Callback<User>() {
@Override
public void success(Result<User> result) {
LogUtils.LOGI("Get success",result.toString());
}
@Override
public void failure(TwitterException e) {
hideProgressDialog();
}
});
通过这种方法我无法获得关注者。
谢谢。
您必须使用更新版本 Retrofit2。
在您的 gradle 文件中写入以下代码:-
compile 'com.squareup.retrofit2:retrofit:2.1.0'
尝试以下代码:
接口:
public interface TwitterFollowersService {
/**
* This method id used to get the List of TWITTER FOLLOWERS.
*
* @param userId Get UserId after login and pass it here
* @param var Send Current user screen name
* @param var1 Weather to skip status accept TRUE/FALSE
* @param var2 Weather to include Entities accept TRUE/FALSE
* @param var3 Count to get number of response
* @return Call object of type FOLLOWERS.
*/
@GET("/1.1/followers/list.json")
Call<Response> show(@Query("user_id") Long userId, @Query("screen_name") String
var, @Query("skip_status") Boolean var1, @Query("include_user_entities") Boolean
var2, @Query("count") Integer var3);
}
在你身上使用这个代码class/Activity:
private Callback<TwitterSession> authorizeCallback = new Callback<TwitterSession>() {
@Override
public void success(com.twitter.sdk.android.core.Result<TwitterSession> result) {
TwitterSession session = Twitter.getSessionManager().getActiveSession();
if (isNetworkAvailable(this)) {
if (session != null) {
TwitterApiServices apiclients = new TwitterApiServices(session);
Call<Response> call = apiclients.getCustomService().show(result.data.getUserId(), result.data.getUserName(), true, false, 100);
call.enqueue(new retrofit2.Callback<Response>() {
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
if (response.body().getUsers().size() > 0) {
ArrayList<TwitterFollowersList> twitterFollowersList = new ArrayList<>();
for (int i = 0; i < response.body().getUsers().size(); i++) {
TwitterFollowersList twitterFollowers = new TwitterFollowersList();
twitterFollowers.setId(response.body().getUsers().get(i).getId());
twitterFollowers.setName(response.body().getUsers().get(i).getName());
twitterFollowers.setProfile_image_url(response.body().getUsers().get(i).getProfile_image_url());
Log.d("TwitterFollowers", "ID: " + response.body().getUsers().get(i).getId() + " Name: " + response.body().getUsers().get(i).getName() + " ProfileImageURL: " + response.body().getUsers().get(i).getProfile_image_url());
twitterFollowersList.add(twitterFollowers);
}
//Upload twitter folowers to server.
uploadUserTwitterContacts(twitterFollowersList);
}
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
showToast(this, "Unable to get response. Try Again!!");
}
});
} else {
showToast(this, getResources().getString(R.string.server_error));
}
} else {
showToast(this, getResources().getString(R.string.no_internet));
}
}
@Override
public void failure(TwitterException e) {
showToast(this, getResources().getString(R.string.server_error));
}
};
使用以下模型 类 作为 响应 ,并使他们的 getter setter:
1)
public class Response {
@SerializedName("users")
private List<Users> users;
private String next_cursor;
private String previous_cursor_str;
private String previous_cursor;
private String next_cursor_str;
}
2)
public class Users {
private String location;
private String default_profile;
private String profile_background_tile;
private String statuses_count;
private String live_following;
private String lang;
private String profile_link_color;
private String profile_banner_url;
private String id;
private String following;
private String muting;
//private String protected;
private String favourites_count;
private String profile_text_color;
private String description;
private String verified;
private String contributors_enabled;
private String profile_sidebar_border_color;
private String name;
private String profile_background_color;
private String created_at;
private String is_translation_enabled;
private String default_profile_image;
private String followers_count;
private String has_extended_profile;
private String profile_image_url_https;
private String geo_enabled;
//private Status status;
private String profile_background_image_url;
private String profile_background_image_url_https;
private String follow_request_sent;
//private Entities entities;
private String url;
private String utc_offset;
private String time_zone;
private String blocking;
private String notifications;
private String profile_use_background_image;
private String friends_count;
private String blocked_by;
private String profile_sidebar_fill_color;
private String screen_name;
private String id_str;
private String profile_image_url;
private String listed_count;
private String is_translator;
}
希望对您有所帮助。 :)
我正在为 Twitter 使用 Fabric SDK。在这里,我可以按照其文档中的描述发出登录请求。现在我不想获取已登录用户的关注者列表并显示在 listview.I 中尝试了很多链接,但这些链接对我不起作用
然后我尝试了这段代码,但每次 运行 我的应用程序仍然崩溃。
class MyTwitterApiClient extends TwitterApiClient
{
public MyTwitterApiClient(TwitterSession session)
{
super(session);
}
public CustomService getCustomService() {
return getService(CustomService.class);
}
interface CustomService {
@GET("/1.1/followers/list.json")
Call<User> show(@Query("user_id") Long userId, @Query("screen_name") String
var, @Query("skip_status") Boolean var1, @Query("include_user_entities") Boolean var2, @Query("count") Integer var3, Callback < Followers > cb);
}
}
这样调用:
new MyTwitterApiClient(session).getCustomService().show(userID, null, true, true, 100, new Callback<User>() {
@Override
public void success(Result<User> result) {
LogUtils.LOGI("Get success",result.toString());
}
@Override
public void failure(TwitterException e) {
hideProgressDialog();
}
});
通过这种方法我无法获得关注者。 谢谢。
您必须使用更新版本 Retrofit2。
在您的 gradle 文件中写入以下代码:-
compile 'com.squareup.retrofit2:retrofit:2.1.0'
尝试以下代码:
接口:
public interface TwitterFollowersService {
/**
* This method id used to get the List of TWITTER FOLLOWERS.
*
* @param userId Get UserId after login and pass it here
* @param var Send Current user screen name
* @param var1 Weather to skip status accept TRUE/FALSE
* @param var2 Weather to include Entities accept TRUE/FALSE
* @param var3 Count to get number of response
* @return Call object of type FOLLOWERS.
*/
@GET("/1.1/followers/list.json")
Call<Response> show(@Query("user_id") Long userId, @Query("screen_name") String
var, @Query("skip_status") Boolean var1, @Query("include_user_entities") Boolean
var2, @Query("count") Integer var3);
}
在你身上使用这个代码class/Activity:
private Callback<TwitterSession> authorizeCallback = new Callback<TwitterSession>() {
@Override
public void success(com.twitter.sdk.android.core.Result<TwitterSession> result) {
TwitterSession session = Twitter.getSessionManager().getActiveSession();
if (isNetworkAvailable(this)) {
if (session != null) {
TwitterApiServices apiclients = new TwitterApiServices(session);
Call<Response> call = apiclients.getCustomService().show(result.data.getUserId(), result.data.getUserName(), true, false, 100);
call.enqueue(new retrofit2.Callback<Response>() {
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
if (response.body().getUsers().size() > 0) {
ArrayList<TwitterFollowersList> twitterFollowersList = new ArrayList<>();
for (int i = 0; i < response.body().getUsers().size(); i++) {
TwitterFollowersList twitterFollowers = new TwitterFollowersList();
twitterFollowers.setId(response.body().getUsers().get(i).getId());
twitterFollowers.setName(response.body().getUsers().get(i).getName());
twitterFollowers.setProfile_image_url(response.body().getUsers().get(i).getProfile_image_url());
Log.d("TwitterFollowers", "ID: " + response.body().getUsers().get(i).getId() + " Name: " + response.body().getUsers().get(i).getName() + " ProfileImageURL: " + response.body().getUsers().get(i).getProfile_image_url());
twitterFollowersList.add(twitterFollowers);
}
//Upload twitter folowers to server.
uploadUserTwitterContacts(twitterFollowersList);
}
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
showToast(this, "Unable to get response. Try Again!!");
}
});
} else {
showToast(this, getResources().getString(R.string.server_error));
}
} else {
showToast(this, getResources().getString(R.string.no_internet));
}
}
@Override
public void failure(TwitterException e) {
showToast(this, getResources().getString(R.string.server_error));
}
};
使用以下模型 类 作为 响应 ,并使他们的 getter setter:
1)
public class Response {
@SerializedName("users")
private List<Users> users;
private String next_cursor;
private String previous_cursor_str;
private String previous_cursor;
private String next_cursor_str;
}
2)
public class Users {
private String location;
private String default_profile;
private String profile_background_tile;
private String statuses_count;
private String live_following;
private String lang;
private String profile_link_color;
private String profile_banner_url;
private String id;
private String following;
private String muting;
//private String protected;
private String favourites_count;
private String profile_text_color;
private String description;
private String verified;
private String contributors_enabled;
private String profile_sidebar_border_color;
private String name;
private String profile_background_color;
private String created_at;
private String is_translation_enabled;
private String default_profile_image;
private String followers_count;
private String has_extended_profile;
private String profile_image_url_https;
private String geo_enabled;
//private Status status;
private String profile_background_image_url;
private String profile_background_image_url_https;
private String follow_request_sent;
//private Entities entities;
private String url;
private String utc_offset;
private String time_zone;
private String blocking;
private String notifications;
private String profile_use_background_image;
private String friends_count;
private String blocked_by;
private String profile_sidebar_fill_color;
private String screen_name;
private String id_str;
private String profile_image_url;
private String listed_count;
private String is_translator;
}
希望对您有所帮助。 :)