Java tutorial
/* * The MIT License (MIT) * Copyright (c) 2014 longkai * The software shall be used for good, not evil. */ package org.catnut.metadata; import android.content.ContentValues; import android.provider.BaseColumns; import android.util.Log; import org.json.JSONArray; import org.json.JSONObject; import org.catnut.core.CatnutMetadata; import org.catnut.util.Constants; /** * ?????? * * @author longkai */ public final class Status implements CatnutMetadata<JSONObject, ContentValues> { private static final String TAG = "Status"; /** ? */ // todo: ???type... public static final String TYPE = "_type"; /** ??id */ public static final String TO_WHICH_TWEET = "_to"; /** ? */ public static final int HOME = 2; /** ?? */ public static final int RETWEET = 3; /** ? */ public static final int PUBLIC = 4; /** */ public static final int COMMENT = 5; /** @? */ public static final int MENTION = 6; /** ? */ public static final int OTHERS = 1; public static final String TABLE = "statuses"; public static final String SINGLE = "status"; public static final String MULTIPLE = "statuses"; public static final String COMMENTS = "comments"; public static final String FAVORITES = "favorites"; public static final String total_number = "total_number"; public static final String MEDIUM_THUMBNAIL = "bmiddle"; public static final String LARGE_THUMBNAIL = "large"; // singleton public static final Status METADATA = new Status(); private Status() { } // ?json public static final String retweeted_status = "retweeted_status"; /** */ public static final String created_at = "created_at"; /** */ public static final String text = "text"; public static final String columnText = "_text"; // keywords... /** ?? */ public static final String source = "source"; /** ?? boolean */ public static final String favorited = "favorited"; /** ? boolean */ public static final String truncated = "truncated"; /** ??ID */ // public static final String in_reply_to_status_id = "in_reply_to_status_id"; /** ??UID */ // public static final String in_reply_to_user_id = "in_reply_to_user_id"; /** ?? */ // public static final String in_reply_to_screen_name = "in_reply_to_screen_name"; /** ???key=thumbnail_pic?"[]" array */ public static final String pic_urls = "pic_urls"; /** ?? */ public static final String thumbnail_pic = "thumbnail_pic"; /** ?? */ public static final String bmiddle_pic = "bmiddle_pic"; /** ?? */ public static final String original_pic = "original_pic"; /** ?????? long */ public static final String retweeted_status_id = "retweeted_status_id"; // todo: geo /** long */ public static final String uid = "uid"; /** ? int */ public static final String reposts_count = "reposts_count"; /** int */ public static final String comments_count = "comments_count"; /** ? int */ public static final String attitudes_count = "attitudes_count"; /** ? */ public static final String mlevel = "mlevel"; // todo: visible // ???????objecttype?0?1??3?4??list_id? // "visible": { // "type": 0, // "list_id": 0 // } @Override public String ddl() { Log.i(TAG, "create table [statuses]..."); StringBuilder ddl = new StringBuilder("CREATE TABLE "); ddl.append(TABLE).append("(").append(BaseColumns._ID).append(" int primary key,").append(TYPE) .append(" int,") // ? .append(TO_WHICH_TWEET).append(" int,") // ??id .append(created_at).append(" text,").append(columnText).append(" text,").append(source) .append(" text,").append(favorited).append(" int,").append(truncated).append(" int,") .append(pic_urls).append(" text,").append(thumbnail_pic).append(" text,").append(bmiddle_pic) .append(" text,").append(original_pic).append(" text,").append(retweeted_status).append(" text,") .append(uid).append(" int,").append(reposts_count).append(" int,").append(comments_count) .append(" int,").append(attitudes_count).append(" int"); return ddl.append(")").toString(); } @Override public ContentValues convert(JSONObject json) { ContentValues tweet = new ContentValues(); tweet.put(BaseColumns._ID, json.optLong(Constants.ID)); tweet.put(created_at, json.optString(created_at)); // ?jsonsql??? tweet.put(columnText, json.optString(text)); tweet.put(source, json.optString(source)); tweet.put(favorited, json.optBoolean(favorited)); tweet.put(truncated, json.optBoolean(truncated)); // ?????? JSONArray thumbs = json.optJSONArray(pic_urls); if (thumbs != null) { // json tweet.put(pic_urls, thumbs.toString()); } tweet.put(thumbnail_pic, json.optString(thumbnail_pic)); tweet.put(bmiddle_pic, json.optString(bmiddle_pic)); tweet.put(original_pic, json.optString(original_pic)); // ??? if (json.has(retweeted_status)) { tweet.put(retweeted_status, json.optJSONObject(retweeted_status).toString()); } // if (json.has(User.SINGLE)) { tweet.put(uid, json.optJSONObject(User.SINGLE).optLong(Constants.ID)); } else if (json.has(uid)) { tweet.put(uid, json.optLong(uid)); } tweet.put(reposts_count, json.optInt(reposts_count)); tweet.put(comments_count, json.optInt(comments_count)); tweet.put(attitudes_count, json.optInt(attitudes_count)); return tweet; } }