I am using Android Annotation
for boilerplate, and Retrofit for Api calls, While doing post requests through retrofit I discovered a Some problems:
When i am calling asynchronous call to "GET" request using Retrofit, I need to do some operation just after my call gets finished, and I can't use "onResponse()" because I am using "Bean" annotation
It doesn't make sense right? have a look on code
Example Bean class:
@EBean
public class someClass{
/* Suppose api is getting RestClient reference from Retrofit*/
@AfterInject
void doSomeWork(){ api = SampleAPI.Factory.getIstance(mcontext);}
/**
* Get list of all courses from database
* @Return List<CourseInfo> courseInfo objects
*/
public List<CourseInfo> GetCoursesList() {
final List<CourseInfo> cor = new ArrayList<>();
api.getAllCourses(user.getApikey()).enqueue(new Callback<List<CourseInfo>>() {
@Override
public void onResponse(retrofit2.Call<List<CourseInfo>> call, Response<List<CourseInfo>> response) {
Collections.copy(cor,response.body());
}
@Override
public void onFailure(retrofit2.Call<List<CourseInfo>> call, Throwable t) {
UiHelpers.showToast(mcontext,"Unable to get List of Course Names");
}
});
return cor;
}
}
Calling in Activity something Like:
@EActivity(R.layout.something)
public class student extends AppCompatActivity {
@Bean
someClass some;
@AfterViews
void setInits(){
course = cc.GetCoursesList();
Toast.makeText(this,"Why this is running before getting all courses??",Toast.LENGTH_LONG).show();
}
}
I want to know how can I improve this structure using Otto? v
And why my this structure is failing?
Because I am unable to get coursesList from server!!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…