Saturday, November 7, 2015

REST API WEEK! App a Day 21: Goodreads Authors

https://github.com/victorman/GoodreadsFavoritesAndroid



Classes Used: Service, AbstractThreadedSyncAdapter, XmlPullParser, ContentProvider, ContentResolver SQLiteOpenHelper, Handler, ContentObserver.

I've always wanted to play with the Goodreads API. So here's my first attempt. Responses are in XML only, unfortunately. This makes a request for the books (reviews) in my favorites shelf and lists the authors. Very simple in theory. I was going to do more but I ran out of time.

I finally made some templates files for a SQLite Database, but without any of the provider's URI constants. Just copy the gist code below into a file template.

package ${PACKAGE_NAME};
#parse("File Header.java")
public class ${NAME} {
public ${NAME}() {}
public static class ${ENTRY_CLASS} implements BaseColumns {
public static final String TABLE_NAME = "${TABLE_NAME}";
public static final String COLUMN_NAME_CREATED = "date_created";
}
}
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import ${PACKAGE_NAME}.${CONTRACT_CLASS}.${ENTRY_CLASS};
#parse("File Header.java")
public class ${NAME} extends SQLiteOpenHelper {
/*
* Database version and name.
*/
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "${DATABASE_NAME}";
/*
* quick database type constants and a comma.
* pay attention to where spaces are inside your strings
* when using constants. you don't want to end up with
* something like " INTEGERNOT NULL" for a type.
*/
private static final String TEXT_TYPE = " TEXT";
private static final String INT_TYPE = " INTEGER";
private static final String REAL_TYPE = " REAL";
private static final String C = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + ${ENTRY_CLASS}.TABLE_NAME + " (" +
${ENTRY_CLASS}._ID + INT_TYPE + " PRIMARY KEY AUTOINCREMENT" + C +
${ENTRY_CLASS}.COLUMN_NAME_CREATED + TEXT_TYPE + C +
/*
* TODO Define columns here
* column definitions are comma seperated
* COLUMN_NAME TYPE (example:)
* name TEXT,
* age INTEGER
*/
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + ${ENTRY_CLASS}.TABLE_NAME;
public ${NAME}(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}

No comments:

Post a Comment