Retrofit + Otto + AA, How to do simple get request?

时光怂恿深爱的人放手 提交于 2019-12-25 18:32:58

问题


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!!


回答1:


Why this is running before getting all courses??

Because that's how asynchronous code works?

Retrofit is not a blocking call.

If you want to perform an action from onResponse back on the UI thread, you don't even need an EventBus library, just give the callback as the parameter of the method.

public void GetCoursesList(Callback<List<CourseInfo>> callback)  {
    api.getAllCourses(user.getApikey()).enqueue(callback);
} 

The method is now void because, again, Retrofit doesn't block, so you returned an empty list while the server request occurred

@AfterViews
void setInits(){

    cc.GetCoursesList(new Callback<List<CourseInfo>>() {
        // TODO: Implement the interface 
     } );

    Toast.makeText(this,"Why this is running before getting all courses??",Toast.LENGTH_LONG).show();

}


来源:https://stackoverflow.com/questions/39055585/retrofit-otto-aa-how-to-do-simple-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!