简体   繁体   中英

Android Twitter Fabric - Persisting a Tweet

Using Android Twitter Fabric SDK to get a Tweet and show it inside of a TweetView or CompactTweetView.

I want to persist a Tweet and then recreate the same Tweet later without asking for the data from Twitter, which could cause an error if asked too many times.

I know I can get the tweet using

import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.tweetui.TweetUtils;


long mTweetId = //some number representing a tweet on Twitter

TweetUtils.loadTweet(mTweetId, this);  //the class implements LoadCallback<Tweet>

//if successful then this method is called
@Override
public void success(Tweet mTweet) 
{
    this.mTweet = mTweet; 

    //I now have the tweet, but I don't want to load it again in the future, instead I would
    //persist it to sharedPreferences or something and then recreate the Tweet calling
    //new Tweet(...) or something that will help me rebuilt the same Tweet which has already
    //been loaded 

}

I can even get some of the parameters of the tweet, but how to easily recreate the same tweet using data that has been persisted already.

We use GSON for serializing/deserializing Tweets in Fabric. It'd be a simple Process for you to use GSON too. Here is a snippet that creates a string as JSON which you could then save where you liked, and creating a Tweet from that string.

final Gson gson = new Gson();
final String tweetJson = gson.toJson(tweet);
final Tweet tweet = gson.fromJson(tweetJson, Tweet.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM