gpt4 book ai didi

java - Robospice + 改造 + ORMLite

转载 作者:行者123 更新时间:2023-11-30 11:14:04 28 4
gpt4 key购买 nike

我将 Robospice 与 Retrofit ans ORMLite 模块一起使用。改造部分工作良好。我有用于改造的城市模型:

城市.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?我想通过 Retrofit 下载数据并通过 ORMLite 将其存储到数据库中。我的 CacheManager 是正确的,即可以正常工作吗?也许我误解了模块 Robospice-ORMLite 是如何工作的?

非常感谢!

最佳答案

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

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

一分钟内所有后续请求都会从这个缓存中获取数据,然后进行网络调用。如果您只想从缓存中获取数据,请查看 getSpiceManager().getFromCache()方法。我想这就是您要找的。

关于java - Robospice + 改造 + ORMLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26106455/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com