我想请您帮忙解决我的错误。我在我的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();
}
}
以下是我 Fragment 中的代码: 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());
}
这是我根据示例代码设置我的Fragment并将其扩展到此的方式。
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;
}
}
This is my TestRequest`
public class TestStringRequest extends RetrofitSpiceRequest<String,RetrofitApi> {
public TestStringRequest() {
super(String.class, RetrofitApi.class);
}
@Override
public String loadDataFromNetwork() throws Exception {
return getService().getTestRetrofit();
}
}
我的 RetrofitApi
@GET(api_reciever+"mytestretrofit")
public String getTestRetrofit();
我对我的代码缺少什么没有任何想法。
期待您的帮助。