2017-06-13 94 views
0

我正在使用twitter4j提取推文列表。這是我的代碼:如何存儲loacally tweets(狀態)列表?

private ResponseList<Status> favouritesSavedLocally; 

ConfigurationBuilder builder = new ConfigurationBuilder(); 

builder.setOAuthConsumerKey(ConstantValues.TWITTER_CONSUMER_KEY); 
builder.setOAuthConsumerSecret(ConstantValues.TWITTER_CONSUMER_SECRET); 
builder.setOAuthAccessToken(SharedPref.getPrefKeyOauthToken()); 
builder.setOAuthAccessTokenSecret(SharedPref.getPrefKeyOauthSecret()); 
builder.setJSONStoreEnabled(true); 
builder.setIncludeEntitiesEnabled(true); 
builder.setIncludeMyRetweetEnabled(true); 

AccessToken accessToken = new AccessToken(SharedPref.getPrefKeyOauthToken(), SharedPref.getPrefKeyOauthSecret()); 
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken); 

try { 
    favouritesSavedLocally = twitter.getFavorites(); 
} catch (TwitterException e) { 
    e.printStackTrace(); 
} 

現在這工作正常......但問題是我需要在本地存儲。有誰知道什麼是存儲狀態(tweet)對象的最佳方式?我試圖將它作爲一個blob存儲在SQLite中,但是當試圖從DB讀取時出現這個錯誤。這是我嘗試以本地存儲它:

private static final String COLUMN_ID = "ID"; 
private static final String COLUMN_FAVOURITE_TWEET = "FAVOURITE_TWEET"; 

... 

@Override 
public void onCreate(SQLiteDatabase sqLiteDatabase) { 
    sqLiteDatabase.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,FAVOURITE_TWEET BLOB) "); 
} 

... 

public boolean addFavouriteStatus(byte[] status) { 

    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COLUMN_FAVOURITE_TWEET, status); 

    long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues); 

    if (result == -1) { 
     return false; 
    } else { 
     sqLiteDatabase.close(); 
     return true; 
    } 
} 

public Cursor getFavouriteStatuses() { 

    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); 
    Cursor result = sqLiteDatabase.rawQuery("select * from " + TABLE_NAME, null); 

    return result; 
} 

我得到嘗試讀取getFavoritesStatuses()

FATAL EXCEPTION: AsyncTask #2 
Process: xxxxxxxx.clientfeed, PID: 11221 
java.lang.RuntimeException: An error occurred while executing doInBackground() 
    at android.os.AsyncTask$3.done(AsyncTask.java:309) 
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 
Caused by: android.database.sqlite.SQLiteException: unknown error (code 0): INTEGER data in nativeGetBlob 
################################################################# 
Error Code : 0 (SQLITE_OK) 
Caused By : unknown error (code 0): INTEGER data in nativeGetBlob 
################################################################# 
    at android.database.CursorWindow.nativeGetBlob(Native Method) 
    at android.database.CursorWindow.getBlob(CursorWindow.java:416) 
    at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45) 
    at xxxxxxxx.ui.TwitterFeedListFragment$GetFavouritesLocalTweets.doInBackground(TwitterFeedListFragment.java:132) 
    at xxxxxxxx.ui.TwitterFeedListFragment$GetFavouritesLocalTweets.doInBackground(TwitterFeedListFragment.java:120) 
    at android.os.AsyncTask$2.call(AsyncTask.java:295) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    ... 4 more 

注意,當這個錯誤:此鳴叫對象的模樣

{"id_str":"873961105277767680","coordinates":null,"in_reply_to_user_id_str":null,"source":"<a href=\"http:\/\/instagram.com\" rel=\"nofollow\">Instagram<\/a>","geo":null,"possibly_sensitive_appealable":false,"in_reply_to_user_id":null,"retweet_count":0,"user":{"default_profile_image":false,"screen_name":"klaussdeejay","profile_link_color":"0084B4","profile_background_color":"C0DEED","is_translator":false,"url":null,"profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/868651398\/9e71de37e9836eb30c0e171028a134d2.jpeg","statuses_count":704,"profile_text_color":"333333","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/3601781438\/76e5351fe2de382e89486e86f6a3bc18_normal.jpeg","following":true,"profile_sidebar_border_color":"C0DEED","verified":false,"id":1396650608,"lang":"en","entities":{"description":{"urls":[]}},"profile_background_tile":false,"listed_count":3,"id_str":"1396650608","profile_sidebar_fill_color":"DDEEF6","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/868651398\/9e71de37e9836eb30c0e171028a134d2.jpeg","protected":false,"translator_type":"none","utc_offset":null,"is_translation_enabled":false,"profile_use_background_image":true,"notifications":false,"geo_enabled":false,"friends_count":366,"default_profile":false,"followers_count":54,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/3601781438\/76e5351fe2de382e89486e86f6a3bc18_normal.jpeg","follow_request_sent":false,"contributors_enabled":false,"has_extended_profile":false,"description":"","time_zone":null,"location":"London, UK","name":"Claudiu Farcas ","created_at":"Thu May 02 08:46:07 +0000 2013","favourites_count":47},"favorited":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"truncated":false,"favorite_count":0,"id":873961105277767680,"is_quote_status":false,"contributors":null,"lang":"en","possibly_sensitive":false,"entities":{"urls":[{"indices":[20,43],"display_url":"instagram.com\/p\/BVNVW5VlNo-\/","expanded_url":"https:\/\/www.instagram.com\/p\/BVNVW5VlNo-\/","url":"https:\/\/t.co\/ngy7BtnvhP"}],"hashtags":[],"user_mentions":[],"symbols":[]},"text":"Just posted a photo https:\/\/t.co\/ngy7BtnvhP","created_at":"Sun Jun 11 17:52:19 +0000 2017","place":null,"in_reply_to_screen_name":null,"retweeted":false} 

預先感謝您!

回答

1

在twitter4j中,Status實際上是Interface,但由於很多twitter4j接口基於JSON,因此您可以將Status轉換爲JSON字符串,反之亦然。您可以將您的Status保存爲字符串。

爲了做到這一點,首先設置JSON存儲在您Configuration爲啓用,並設置它:

ConfigurationBuilder cb = ConfigurationBuilder(); 
... 
cb.setJSONStoreEnabled(true); 
Configuration configuration = cb.build(); 
// Setup configuration 

,你可以開始將它們轉換:

String json = TwitterObjectFactory.getRawJSON(status); // Status -> JSON String 

Status status = TwitterObjectFactory.createStatus(json); // JSON String -> Status 

所以現在你可以在你的數據庫中保存Status作爲String酶。

對於ConfigurationBuilder設置Configuration,讀到這裏:

還檢查了一些twitter4j單證: