We have used a Activity-BL-DAO-DB(sqlite) in my app while developing.
Due to change in requirement, we have to use REST service from the server alone. I have seen Retrofit for it. But I'm not sure how to use it in DAO classes instead of SQL queries.
We have looked into bus concepts which requires more rework. We wanted to make minimal changes to the code to incorporate this change.
If anything else needed,let me know.
Eg: Following is the sample flow which will display list of technologies in the list.
Technology Activity OnCreate method:
techList=new ArrayList<Technology>();
techList=technologyBL.getAllTechnology(appId);
adapterTech=new TechnologyAdapter(this,new ArrayList<Technology> (techList));
listView.setAdapter(adapterTech);
Technology BL :
public List<Technology> getAllTechnology(String appId) {
techList=technologyDao.getAllTechnology(appId);
// some logic
return techList;
}
Technology DAO:
public List<Technology> getAllTechnology(String appId) {
//sql queries
return techList;
}
Technology Model:
class Technology{
String id,techName,techDescription;
//getters & setters
}
I have to replace sql queries with retrofit request. I have created the following retrofit class and interfaces:
RestClient Interface:
public interface IRestClient {
@GET("/apps/{id}/technologies")
void getTechnoloies(@Path("id") String id,Callback<List<Technology>> cb);
//Remaining methods
}
RestClient :
public class RestClient {
private static IRestClient REST_CLIENT;
public static final String BASE_URL = "http://16.180.48.236:22197/api";
Context context;
static {
setupRestClient();
}
private RestClient() {}
public static RestClient get() {
return REST_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.setClient(getClient())
.setRequestInterceptor(new RequestInterceptor() {
//cache related things
})
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
REST_CLIENT = restAdapter.create(IAluWikiClient.class);
}
private static OkClient getClient(){
//cache related
}
}
I tried calling with both sync/async methods in DAO. For sync method,it was throwing some error related to main thread.For Async,it is crashing as request is done late.
Sync call in DAO:
techList=RestClient.get().getTecchnologies(id);
Async call in DAO:
RestClient.get().getTechnolgies(id,new CallBack<List<Technolgy>(){
@Override
public void success(List<Technology> technologies, Response response) {
techList=technologies;
}
@Override
public void failure(Retrofit error){}
});