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)],,,