美文网首页
学习Retrofit笔记

学习Retrofit笔记

作者: android难民 | 来源:发表于2015-11-24 17:07 被阅读1374次

本文是基于Retrofit2进行学习。首先你要知道Retrofit是一个网络请求框架,它的api 定义可以查阅官网.

从零开始搭建一个Retrofit的例子

以下示例会分别列出Retrofit 1.9Retrofit 2的区别用法

添加依赖

Retrofit 1.9

dependencies {  
    // Retrofit & OkHttp
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.2.0'
}

Retrofit 2

dependencies {  
    // Retrofit & OkHttp
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
}

Api服务核心类

ServiceGenerator是项目的http核心类,专门用于封装http请求的基类。

Retrofit 1.9

public class ServiceGenerator {

    public static final String API_BASE_URL = "http://your.api-base.url";

    private static RestAdapter.Builder builder = new RestAdapter.Builder()
                .setEndpoint(API_BASE_URL)
                .setClient(new OkClient(new OkHttpClient()));

    public static <S> S createService(Class<S> serviceClass) {
        RestAdapter adapter = builder.build();
        return adapter.create(serviceClass);
    }
}

Retrofit 2

public class ServiceGenerator {
    public static final String API_BASE_URL = "http://api.github.com";

    private static OkHttpClient httpClient = new OkHttpClient();
    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass) {
        Retrofit retrofit = builder.client(httpClient).build();
        return retrofit.create(serviceClass);
    }
}

Json解析

Retrofit 1.9默认带有Gson的支持。Retorfit2就需要自定义添加支持。

compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' 

Api调用范例

这里我会以github的API为例。

声明GitHubClient类

Retrofit 1.9

public interface GitHubClient {  
    @GET("/repos/{owner}/{repo}/contributors")
    List<Contributor> contributors(
        @Path("owner") String owner,
        @Path("repo") String repo
    );
}

Retrofit 2

public interface GitHubClient {  
    @GET("/repos/{owner}/{repo}/contributors")
    Call<List<Contributor>> contributors(
        @Path("owner") String owner,
        @Path("repo") String repo
    );
}

Model类

class Contributor {  
    String login;
    int contributions;
}

请求

Retrofit 1.9

    GitHubClient client = ServiceGenerator.createService(GitHubClient.class);

    // Fetch and print a list of the contributors to this library.
    List<Contributor> contributors =
        client.contributors("fs_opensource", "android-boilerplate");

    for (Contributor contributor : contributors) {
        System.out.println(
                contributor.login + " (" + contributor.contributions + ")");
    }

Retrofit 2

    GitHubClient client = ServiceGenerator.createService(GitHubClient.class);

        // Fetch and print a list of the contributors to this library.
        Call<List<Contributor>> call =
                client.contributors("lzyzsd", "Awesome-RxJava");

        List<Contributor> contributors = null;
        try {
            contributors = call.execute().body();
            for (Contributor contributor : contributors) {
                System.out.println(
                        contributor.login + " (" + contributor.contributions + ")");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

相关文章

  • Retrofit入门笔记

    Retrofit 是 square 的一个网络请求框架,本文记录Retrofit的学习笔记 Retrofit请求框...

  • Retrofit学习笔记

    Retrofit学习笔记 一、什么是Retrofit框架 Retrofit框架是Square公司出品的目前非常流行...

  • Retrofit 学习笔记

    参考: 网络加载框架 - Retrofit 这是一份很详细的 Retrofit 2.0 使用教程(含实例讲解) A...

  • retrofit 学习笔记

    ps: 我写这篇这是很晚了,也是怪我自己啊,一直以来拖拖拉拉的就是改不了,retrofit 看着好几次了,看完有些...

  • Retrofit学习笔记

    一、Retrofit简介 Retrofit是Square公司开发的一款针对Android网络请求的框架,Retro...

  • 学习Retrofit笔记

    本文是基于Retrofit2进行学习。首先你要知道Retrofit是一个网络请求框架,它的api 定义可以查阅官网...

  • Retrofit学习笔记

    注:以下内容为本人自己做(chao)的学习笔记,可能含有少儿不宜的误导性内容,学习Retrofit请看参考文章。。...

  • Retrofit 学习笔记

    前言 Retrofit是个极其优秀的库,特别是和rxjava结合起来,使用起来那是一个丝滑般爽。不过使用了一两年,...

  • OkHttp+Retrofit+RxJava 实现过期Token

    在经历了OkHttp、Retrofit、RxJava的学习后,终于可以开始写代码rua!附我的学习笔记:https...

  • Retrofit 学习笔记(转载)

    转自 你真的会用Retrofit2吗?Retrofit2完全教程 一、使用步骤 1、在 Gradle加入Retro...

网友评论

      本文标题:学习Retrofit笔记

      本文链接:https://www.haomeiwen.com/subject/llirhttx.html