Robospice + Retrofit + ORMLite

时间:2014-09-29 18:28:16

标签: java android ormlite retrofit robospice

我使用Robospice和Retrofit和ORMLite模块。改造部分工作良好。我有改造城市模型:

City.java:

public class City {
    public int city_id;
    public String name;

    @SuppressWarnings("serial")
    public static class List extends ArrayList<City> {
    }
}

我是通过GET-request从服务器获取此模型的:

MyApi.java

public interface MyAPI {
    @GET("/cities")
    City.List getCities();
}

这部分通过调用此方法正常工作:

getSpiceManager().execute(mRequestCity, "city", DurationInMillis.ONE_MINUTE, new ListCityRequestListener());

和听众:

public final class ListCityRequestListener implements RequestListener<City.List> {

    @Override
    public void onRequestFailure(SpiceException spiceException) {
        Toast.makeText(RegisterActivity.this, "failure", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestSuccess(final City.List result) {
        Toast.makeText(RegisterActivity.this, "success", Toast.LENGTH_SHORT).show();
        updateCities(result);
    }
}

此时我想从服务器下载城市列表一次,并通过ORMLite模块将此列表存储到sqlitedb中。我创建了ORMLite模型:

City.java

@DatabaseTable(tableName = "city")
public class City {
    public final static String DB_CITY_ID_FIELD_NAME = "id";
    public final static String DB_CITY_NAME_FIELD_NAME = "name";

    @DatabaseField(canBeNull =  false, dataType = DataType.INTEGER, columnName = DB_CITY_ID_FIELD_NAME)
    int id;

    @DatabaseField(canBeNull = false, dataType = DataType.STRING, columnName = DB_CITY_NAME_FIELD_NAME)
    private String name;

    public City() {

    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("id = ").append(id);
        sb.append(", ").append("name = ").append(name);

        return sb.toString();
    }
}

我的 RetrofitSpiceService.java 如下所示:

public class RetrofitSpiceService extends RetrofitGsonSpiceService {

    private final static String BASE_URL = "http://example.com/api/v1";
    private final static UserFunctions userFunctions = new UserFunctions();

    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        List< Class< ? >> classCollection = new ArrayList< Class< ? >>();

        // add persisted classes to class collection
        classCollection.add( City.class );

        // init
        RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper( application, "sample_database.db", 1 );
        InDatabaseObjectPersisterFactory inDatabaseObjectPersisterFactory = new InDatabaseObjectPersisterFactory( application, databaseHelper, classCollection );
        cacheManager.addPersister( inDatabaseObjectPersisterFactory );

        return cacheManager;
    }

    @Override
    protected Builder createRestAdapterBuilder() {
        Builder mBuilder = super.createRestAdapterBuilder();

        mBuilder.setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                if (userFunctions.isUserLoggedIn()) {
                    request.addHeader("Authorization", userFunctions.getToken());
                }
            }
        });

        return mBuilder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        addRetrofitInterface(MyAPI.class);
    }

    @Override
    protected String getServerUrl() {
        return BASE_URL;
    }
}

我无法理解如何在 City 数据库中存储和读取数据?我如何更改RetrofitSpiceService?我想通过 Retrofit 下载数据,并通过 ORMLite 将其存储到数据库中。我的CacheManager是正确的,即能正常工作吗?也许我误解了模块 Robospice-ORMLite 是如何工作的?

非常感谢!

1 个答案:

答案 0 :(得分:1)

当您使用缓存键和持续时间进行execute()调用时,Robospice会将您的响应存储到数据库中。

getSpiceManager().execute(mRequestCity, "city", DurationInMillis.ONE_MINUTE, new ListCityRequestListener());

一分钟内的所有后续请求都将从此缓存中获取数据,然后进行网络呼叫。如果您只想从缓存中获取数据,请查看getSpiceManager().getFromCache()方法。我认为这就是你要找的东西。