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;
    }

结束

相关推荐