Retrofix 2 基础例子 Github

Repo 案例
包结构

build.gradle

dependencies {
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:retrofit-adapters:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
}

Contributor.java 用Gson转换对象

package com.wdxxl.retrofit2.bean;

public class Contributor {
    public String login;
    public int contributions;

    public Contributor(String login, int contributions) {
        this.login = login;
        this.contributions = contributions;
    }
    @Override
    public String toString() {
        return "Contributor [login=" + login + ", contributions=" + contributions + "]";
    }
}

Repo.java 用Gson转换对象

package com.wdxxl.retrofit2.bean;

public class Repo {
    private final long id;
    private final String full_name;
    private Owner owner;

    public Repo(long id, String full_name) {
        this.id = id;
        this.full_name = full_name;
    }

    @Override
    public String toString() {
        return "Repo [id=" + id + ", full_name=" + full_name + ", " + owner.toString() + "]";
    }

    class Owner {
        private final long id;
        private final String login;

        public Owner(long id, String login) {
            this.id = id;
            this.login = login;
        }

        @Override
        public String toString() {
            return "Owner (id=" + id + ", login=" + login + ")";
        }
    }
}

GithubService.java

package com.wdxxl.retrofit2.service;

import java.util.List;

import com.wdxxl.retrofit2.bean.Contributor;
import com.wdxxl.retrofit2.bean.Repo;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

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

    @GET("/users/{user}/repos")
    Call<List<Repo>> getRepos(@Path("user") String user);
}

GithubServiceImpl.java 主程序测试入口

package com.wdxxl.retrofit2.service.impl;

import java.io.IOException;
import java.util.List;

import com.wdxxl.retrofit2.bean.Contributor;
import com.wdxxl.retrofit2.bean.Repo;
import com.wdxxl.retrofit2.service.GithubService;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class GithubServiceImpl {
    public static void main(String[] args) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("https://api.github.com/")
                .build();

        GithubService service = retrofit.create(GithubService.class);

        getContributors(service);
        getRepos(service);
    }

    public static void getContributors(GithubService service){
        Call<List<Contributor>> call = service.getContributors("wdxxl","wdxxl.github.io");
        try {
            Response<List<Contributor>> response = call.execute();
            System.out.println(response.body().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void getRepos(GithubService service){
        Call<List<Repo>> call = service.getRepos("wdxxl");
        try {
            Response<List<Repo>> response = call.execute();
            System.out.println(response.body().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出结果

[Contributor [login=wdxxl, contributions=15]]
[Repo [id=42148922, full_name=wdxxl/Alternator, Owner (id=1063750, login=wdxxl)], 
Repo [id=53124093, full_name=wdxxl/books, Owner (id=1063750, login=wdxxl)],,,
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,869评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,076评论 25 708
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,941评论 6 342
  • 很多人喜欢独立的女生,也总会厌倦那些衣来伸手饭来张口什么都不会做的阔太太,尽管那些所谓的阔太太只是因为当时所谓的一...
    诗语未落阅读 105评论 0 1
  • 中介者模式 mvc的模型,Meditor控制
    小酷哥阅读 236评论 0 0