f_retrofit_基本使用_20210226

官方文档(github.io)

A type-safe HTTP client for Android and Java

Retrofit requires at minimum Java 8+ or Android API 21+.

引入依赖

MAVEN

<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>retrofit</artifactId>
  <version>(insert latest version)</version>
</dependency>
        <!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit -->
        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>retrofit</artifactId>
            <version>2.9.0</version>
        </dependency>

GRADLE

implementation 'com.squareup.retrofit2:retrofit:(insert latest version)'

转换库

例如GSON

implementation 'com.squareup.retrofit2:converter-gson:(insert latest version)'
        <!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson -->
        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>converter-gson</artifactId>
            <version>2.9.0</version>
        </dependency>

基本流程

0:定义实体类
1:定义接口
3:使用Retrofit对象生成接口实例
2:创建Retrofit对象
4:调用接口方法获得Call对象
5:使用enqueue方法异步网络请求
5:使用execute方法同步网络请求

使用范例

定义实体类

public class Repo {
    private int id;
    private String name;
    @SerializedName("html_url")
    private String htmlUrl;

    @Override
    public String toString() {
        return "Repo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", htmlUrl='" + htmlUrl + '\'' +
                '}';
    }
  // 省略getter和setter方法
}

定义接口

public interface GitHubService {
    @GET("users/{user}/repos")
    Call<List<Repo>> listRepos(@Path("user") String user);
}

创建Retrofit对象

    public static void main(String[] args) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
      	// 省略其他部分代码
   }

生成接口实例

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

获得Call对象

Call<List<Repo>> repos = service.listRepos("Square");

异步或同步请求

        repos.enqueue(new Callback<List<Repo>>() {
            @Override
            public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
                for (Repo repo : response.body()) {
                    System.out.println(repo);
                }
            }

            @Override
            public void onFailure(Call<List<Repo>> call, Throwable throwable) {
                System.out.println("访问失败!");
            }
        });
        System.out.println("main线程结束!");

On Android, callbacks will be executed on the main thread. On the JVM, callbacks will happen on the same thread that executed the HTTP request.

运行效果

main线程结束!
Repo{id=36665193, name='Aardvark', htmlUrl='https://github.com/square/Aardvark'}
Repo{id=60724575, name='Ackbar', htmlUrl='https://github.com/square/Ackbar'}
Repo{id=89733456, name='active_merchant_square', htmlUrl='https://github.com/square/active_merchant_square'}
Repo{id=48068599, name='active_record-sql_analyzer', htmlUrl='https://github.com/square/active_record-sql_analyzer'}
Repo{id=7923123, name='android-times-square', htmlUrl='https://github.com/square/android-times-square'}
Repo{id=271357426, name='anvil', htmlUrl='https://github.com/square/anvil'}
Repo{id=12226292, name='apropos', htmlUrl='https://github.com/square/apropos'}
//...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值