Retrofit methods can be declared for either synchronous or asynchronous execution.
A method with a return type will be executed synchronously.
@GET("/user/{id}/photo")
Photo getUserPhoto(@Path("id") int id);
Asynchronous execution requires the last parameter of the method be a Callback
.
@GET("/user/{id}/photo")
void getUserPhoto(@Path("id") int id, Callback<Photo> cb);
On Android, callbacks will be executed on the main thread. For desktop applications callbacks will happen on the same thread that executed the HTTP request.
Retrofit also integrates RxJava to support methods with a return type of rx.Observable
@GET("/user/{id}/photo")
Observable<Photo> getUserPhoto(@Path("id") int id);
Observable requests are subscribed asynchronously and observed on the same thread that executed the HTTP request. To observe on a different thread (e.g. Android's main thread) call observeOn(Scheduler)
on the returned Observable
.
Note: The RxJava integration is experimental.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…