我想请求帮助解决我的错误。我为我的api使用了robospice-retrofit,我想获得缓存。在我的其他示例程序中,我可以获取缓存和值,但是当我在片段中使用它时,我总是具有缓存的空值。顺便说一下,我创建了另一个可以处理我所有请求的类,这有问题吗?
请检查我的代码:这是从单独的课程中创建的。
public void roboretroGetTempString(final DomainResponseCallback callback,SpiceManager spiceManager){
boolean isHasCache = false;
TestStringRequest graphRequest = new TestStringRequest();
try {
if(spiceManager.isDataInCache(String.class,"graph",DurationInMillis.ONE_MINUTE).get()){
Log.e(TAG,"onRefresh:Has Cache"+spiceManager.getDataFromCache(String.class,"graph"));
}else{
/*Execute if cache has expired*/
Log.e(TAG,"onRefresh:No Cache");
spiceManager.execute(graphRequest, "graph", DurationInMillis.ONE_MINUTE, new RequestListener<String>() {
@Override
public void onRequestFailure(SpiceException spiceException) {
spiceException.printStackTrace();
callback.onApiResponse("Error", null);
}
@Override
public void onRequestSuccess(String str) {
Log.e(TAG,"onRequestSuccess:result-"+str);
}
});
}
} catch (CacheCreationException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (CacheLoadingException e) {
e.printStackTrace();
}
}
这是我片段中的代码:DomainResponseCallback是我的回调接口。我还将SpiceManager作为我在上面代码中使用的参数传递。
private void roboLoadTest(){
GraphController graphController = new GraphController();
graphController.roboretroGetTempString(new DomainResponseCallback() {
@Override
public void onApiResponse(String result, Object obj) {
progressBar.setVisibility(View.GONE);
Log.e(TAG,"onApiResponse-"+result);
}
@Override
public void onApiError(String error) {
Log.e(TAG,"onApiResponse-"+error);
}
},getSpiceManager());
}
这是我如何基于示例代码设置我的片段并将我的片段扩展到此。
public class RoboFragment extends Fragment{
private String TAG = RoboFragment.class.getSimpleName();
/***
* With {@link com.octo.android.robospice.UncachedSpiceService} there is no cache management. Remember to declare it in
* AndroidManifest.xml
*/
private SpiceManager spiceManager = new SpiceManager(RoboSpiceService.class);
@Override
public void onStart() {
super.onStart();
spiceManager.start(getActivity());
}
@Override
public void onStop() {
// Please review https://github.com/octo-online/robospice/issues/96 for the reason of that
// ugly if statement.
if (spiceManager.isStarted()) {
spiceManager.shouldStop();
}
super.onStop();
}
protected SpiceManager getSpiceManager() {
return spiceManager;
}
}
这是我的TestRequest`
public class TestStringRequest extends RetrofitSpiceRequest<String,RetrofitApi> {
public TestStringRequest() {
super(String.class, RetrofitApi.class);
}
@Override
public String loadDataFromNetwork() throws Exception {
return getService().getTestRetrofit();
}
}
My RetrofitApi
@GET(api_reciever+"mytestretrofit")
public String getTestRetrofit();
我对我的代码中遗漏的内容一无所知。
期待您的协助。
答案 0 :(得分:2)
每次打开新片段时,都会创建/启动一个新的spice管理器,并且spice管理器也将关闭。这就是为什么你将有一个空/空缓存。查看[post]。1
在我的场景中,我刚创建了一个单独的应用程序扩展,并在我的主要活动(片段的父级)中创建了spicemanager。它的工作原理。我不知道这是否是最佳解决方案,仍在寻找更好的方法。
public class AppSingleton extends Application{
private static AppSingleton ourInstance = new AppSingleton();
private SpiceManager spiceManager= new SpiceManager(RoboSpiceService.class);
public static AppSingleton getInstance() {
return ourInstance;
}
public SpiceManager getSpiceManager() {
return spiceManager;
}
}