Android网络请求框架Retrofit使用笔记
Retrofit是一个安卓端开源网络请求库,目前依赖于okhttp:
http://square.github.io/retrofit/
并提供了多种数据转换工厂和RxJava适配工厂接口。例如:
compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1'
使用retrofit访问接口:
void test0() {
//创建Retrofit对象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
GithubService service = retrofit.create(GithubService.class);
service.getContributors("square", "retrofit").enqueue(
new Callback<List<ContributorBean>>() {
@Override
public void onResponse(Call<List<ContributorBean>> call
, Response<List<ContributorBean>> response) {
for (ContributorBean con : response.body()) {
Log.e("TAG", con.login);
}
}
@Override
public void onFailure(Call<List<ContributorBean>> call
, Throwable t) {
t.printStackTrace();
}
});
}
interface GithubService {
//https://api.github.com/repos/{owner}/{repo}/contributors
@GET("/repos/{owner}/{repo}/contributors")
Call<List<ContributorBean>> getContributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
class ContributorBean {
String login;
String type;
}演示了一个带参Get请求,使用Gson做json数据转换。
若要加入RxJava来做事件处理:
void test0() {
//创建Retrofit对象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
GithubService service = retrofit.create(GithubService.class);
service.getContributors("square", "retrofit")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<ContributorBean>>() {
@Override
public void call(List<ContributorBean> contributorBeans) {
for (ContributorBean con : contributorBeans) {
Log.e("TAG", con.login);
}
}
});
}
interface GithubService {
//https://api.github.com/repos/{owner}/{repo}/contributors
@GET("/repos/{owner}/{repo}/contributors")
Observable<List<ContributorBean>> getContributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
class ContributorBean {
String login;
String type;
}产生了同样的效果。
使用flatMap实现链式调用:访问接口1->接口1返回后->访问接口2->接口2返回
void test0() {
//创建Retrofit对象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
final GithubService service = retrofit.create(GithubService.class);
service.getContributors("square", "retrofit")
.flatMap(new Func1<List<ContributorBean>, Observable<List<EventBean>>>() {
@Override
public Observable<List<EventBean>> call(List<ContributorBean> contributorBeans) {
for (ContributorBean con : contributorBeans) {
Log.e("TAG", con.login);
}
return service.getEvents();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<EventBean>>() {
@Override
public void call(List<EventBean> eventBeans) {
for (EventBean eve : eventBeans) {
Log.e("TAG", eve.created_at);
}
}
});
}
interface GithubService {
//https://api.github.com/repos/{owner}/{repo}/contributors
@GET("/repos/{owner}/{repo}/contributors")
Observable<List<ContributorBean>> getContributors(
@Path("owner") String owner,
@Path("repo") String repo);
//https://api.github.com/events
@GET("events")
Observable<List<EventBean>> getEvents();
}
class EventBean {
String id;
String type;
String created_at;
}
class ContributorBean {
String login;
String type;
}结束
相关推荐
冰川孤辰 2019-09-08
xzw 2019-07-01
smaillift 2019-07-01
Palingenesis 2019-06-26
OliverLau 2019-06-21
zhangjp 2017-10-17
Miryou 2019-06-26
霸气的名字 2020-06-23
smaillift 2020-02-17
霸气的名字 2020-02-15
RikkaTheWorld 2019-12-08
TOmyhonour 2019-11-08
齐天大圣数据候 2019-10-31
kcstrong 2019-10-19
kangtingting0 2019-09-07
俊光 2018-08-10
俊光 2019-07-30
javashu0 2019-07-01
needh 2019-06-30