关于android使用retrofit2 demo遇到的问题

本文介绍了一个使用Retrofit框架调用GitHub API的具体示例。通过创建REST适配器并定义GitHub API接口,实现了获取指定项目的贡献者信息的功能。此外,还解决了在Android项目中引入Gson转换器依赖的问题。
public final class SimpleService {
  public static final String API_URL = "https://api.github.com";
   
  public static class Contributor {
  public final String login;
  public final int contributions;
   
  public Contributor(String login, int contributions) {
  this.login = login;
  this.contributions = contributions;
  }
  }
   
  public interface GitHub {
  @GET("/repos/{owner}/{repo}/contributors")
  Call<List<Contributor>> contributors(
  @Path("owner") String owner,
  @Path("repo") String repo);
  }
   
  public static void main(String... args) throws IOException {
  // Create a very simple REST adapter which points the GitHub API.
  Retrofit retrofit = new Retrofit.Builder()
  .baseUrl(API_URL)
  .addConverterFactory(GsonConverterFactory.create())
  .build();
   
  // Create an instance of our GitHub API interface.
  GitHub github = retrofit.create(GitHub.class);
   
  // Create a call instance for looking up Retrofit contributors.
  Call<List<Contributor>> call = github.contributors("square", "retrofit");
   
  // Fetch and print a list of the contributors to the library.
  List<Contributor> contributors = call.execute().body();
  for (Contributor contributor : contributors) {
  System.out.println(contributor.login + " (" + contributor.contributions + ")");
  }
  }
  }

把代码转移到android项目中去,GsonConverterFactory会找不到。


compile 'com.squareup.retrofit2:retrofit:2.0.1'
在导入这个的基础上需要再导入一个
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值