2014-09-29 64 views
2

我正在使用Robospice和Retrofit ans ORMLite模塊。改造部分工作良好。我對改造城市模型: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請求採取從服務器這種模式:

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; 
    } 
} 

我不明白我怎麼可以存儲和從我的城市數據庫讀取數據?我如何需要更改RetrofitSpiceService?我想通過改裝下載數據並通過ORMLite將其存儲到數據庫。我的CacheManager是正確的,即將正常工作?也許我誤解了模塊Robospice-ORMLite是如何工作的?

非常感謝!

回答

1

當您使用緩存鍵和持續時間進行execute()調用時,Robospice會將您的響應存儲到數據庫中。

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

所有以下請求在一分鐘內將從此緩存中獲取數據,然後進行網絡調用。如果你只想從緩存中獲取數據,請看getSpiceManager().getFromCache()方法。我認爲這是你正在尋找的。

+0

這意味着我可以只使用緩存,沒有sqlite?我認爲這在用戶關閉並再次打開應用程序時不起作用。在將來,我想獲得時間戳,指示城市表在服務器上更改的時間。我會檢查這個時間,如果它大於我在sqlite中的時間戳,我將再次從服務器下載數據。你能舉一些例子如何一起使用Retrofit&ormlite嗎?網絡請求僅在Retrofit模塊中需要。 – user3551221 2014-09-30 07:24:05

+0

1.當你在createCacheManager()中傳遞它時,Robospice的緩存使用ORMLite。如果您強制關閉應用程序並再次打開它,數據仍將保存在緩存中。 2.您需要使用If-Modified-Since/Last-Modified緩存實施。所以如果數據沒有修改,服務器返回304. 請看看Robospice文檔,它涵蓋了很多問題。 – 2014-09-30 09:29:28