Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...