學(xué)習(xí)Retrofit筆記

本文是基于Retrofit2進(jìn)行學(xué)習(xí)襟雷。首先你要知道Retrofit是一個(gè)網(wǎng)絡(luò)請求框架阱表,它的api 定義可以查閱官網(wǎng).

從零開始搭建一個(gè)Retrofit的例子

以下示例會(huì)分別列出Retrofit 1.9Retrofit 2的區(qū)別用法

添加依賴

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服務(wù)核心類

ServiceGenerator是項(xiàng)目的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默認(rèn)帶有Gson的支持抑月。Retorfit2就需要自定義添加支持。

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

Api調(diào)用范例

這里我會(huì)以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();
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末洞慎,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子嘿棘,更是在濱河造成了極大的恐慌,老刑警劉巖旭绒,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鸟妙,死亡現(xiàn)場離奇詭異焦人,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)重父,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門花椭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人房午,你說我怎么就攤上這事矿辽。” “怎么了郭厌?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵袋倔,是天一觀的道長。 經(jīng)常有香客問我折柠,道長宾娜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任扇售,我火速辦了婚禮前塔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘承冰。我一直安慰自己华弓,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布困乒。 她就那樣靜靜地躺著寂屏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪顶燕。 梳的紋絲不亂的頭發(fā)上凑保,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音涌攻,去河邊找鬼欧引。 笑死,一個(gè)胖子當(dāng)著我的面吹牛恳谎,可吹牛的內(nèi)容都是我干的芝此。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼因痛,長吁一口氣:“原來是場噩夢啊……” “哼婚苹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鸵膏,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤膊升,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后谭企,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體廓译,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡评肆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了非区。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瓜挽。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖征绸,靈堂內(nèi)的尸體忽然破棺而出久橙,到底是詐尸還是另有隱情,我是刑警寧澤管怠,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布淆衷,位于F島的核電站,受9級(jí)特大地震影響排惨,放射性物質(zhì)發(fā)生泄漏吭敢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一暮芭、第九天 我趴在偏房一處隱蔽的房頂上張望鹿驼。 院中可真熱鬧,春花似錦辕宏、人聲如沸畜晰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凄鼻。三九已至,卻和暖如春聚假,著一層夾襖步出監(jiān)牢的瞬間块蚌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國打工膘格, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留峭范,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓瘪贱,卻偏偏與公主長得像纱控,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子菜秦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容