I am trying to explore Retrofit+OkHttp on Android. Here's some code I found online :
我正在尝试在Android上探索Retrofit + OkHttp。这是我在网上找到的一些代码:
RestAdapter restAdapter = new RestAdapter.Builder().setExecutors(executor, executor)
.setClient(new OkClient(okHttpClient))
.setServer("blah").toString())
.build();
If I don't use executor service, will my code be running on the main thread ? Should I make web requests in a new thread hence ?
如果我不使用执行程序服务,我的代码是否会在主线程上运行?我应该在新线程中发出Web请求吗?
2 个解决方案
#1
17
The method that return a value does it Synchronously.
返回值的方法是同步的。
@GET("/user/{id}/asset")
Asset getUserAsset(@Path("id") int id);
To do it Asynchronous all you need is to add a Callback.
要做到这一点异步你只需要添加一个回调。
@GET("/user/{id}/asset")
void getUserAsset(@Path("id") int id, Callback<Asset> cb);
Hope this Helps.
希望这可以帮助。
Regards!
#2
104
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
.
异步执行要求方法的最后一个参数是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.
在Android上,回调将在主线程上执行。对于桌面应用程序,回调将在执行HTTP请求的同一线程上发生。
Retrofit also integrates RxJava to support methods with a return type of rx.Observable
Retrofit还集成了RxJava,以支持返回类型为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
.
可观察请求是异步订阅的,并在执行HTTP请求的同一线程上观察到。要在不同的线程(例如Android的主线程)上观察,请在返回的Observable上调用observeOn(Scheduler)。
Note: The RxJava integration is experimental.
注意:RxJava集成是实验性的。
#1
17
The method that return a value does it Synchronously.
返回值的方法是同步的。
@GET("/user/{id}/asset")
Asset getUserAsset(@Path("id") int id);
To do it Asynchronous all you need is to add a Callback.
要做到这一点异步你只需要添加一个回调。
@GET("/user/{id}/asset")
void getUserAsset(@Path("id") int id, Callback<Asset> cb);
Hope this Helps.
希望这可以帮助。
Regards!
#2
104
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
.
异步执行要求方法的最后一个参数是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.
在Android上,回调将在主线程上执行。对于桌面应用程序,回调将在执行HTTP请求的同一线程上发生。
Retrofit also integrates RxJava to support methods with a return type of rx.Observable
Retrofit还集成了RxJava,以支持返回类型为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
.
可观察请求是异步订阅的,并在执行HTTP请求的同一线程上观察到。要在不同的线程(例如Android的主线程)上观察,请在返回的Observable上调用observeOn(Scheduler)。
Note: The RxJava integration is experimental.
注意:RxJava集成是实验性的。