美文网首页
Retrofit--->基本使用

Retrofit--->基本使用

作者: 谢尔顿 | 来源:发表于2018-07-05 12:06 被阅读10次

引言

这里只是一个官方的关于Retrofit简单使用的代码,具体关于其的深奥分析,可以看我贴出的参考文章。

1.定义网络请求的接口

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

2.创建retrofit对象,并且获取网络请求接口的实例

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

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

3.调用网络请求接口的方法进行请求

Call<List<Repo>> repos = service.listRepos("octocat");
        //异步请求
        repos.enqueue(new Callback<List<Repo>>() {
            @Override
            public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
                
            }

            @Override
            public void onFailure(Call<List<Repo>> call, Throwable t) {

            }
        });

        //同步请求
        try {
            List<Repo> body = repos.execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }

参考文章:

相关文章

  • Retrofit--->基本使用

    引言 这里只是一个官方的关于Retrofit简单使用的代码,具体关于其的深奥分析,可以看我贴出的参考文章。 1.定...

  • Flutter--Text/Container/Image

    Text基本使用 Container基本使用 Image基本使用

  • 基本使用

    1、 打开需要上传的文件夹执行: git init 格式化窗口 2、执行 git add . 上传文件 3、执行 ...

  • 基本使用

    href="javascript:;" 其中javascript: 是一个伪协议。它可以让我们通过一个链接来调用...

  • 基本使用

    数据库: 什么是数据库?简单来说就是存数据的。 都有什么是数据库? oracle(强大,跟金融政府打交道的,安全,...

  • 基本使用

    本文参考:https://morvanzhou.github.io/tutorials/machine-learn...

  • 6-xpath和css select基本使用

    Xpath基本使用 css select基本使用

  • MySQL语法入门(一)

    MySQL语法入门(一) 基本运算符使用 基本数学函数使用 基本字符串函数使用 基本日期时间函数使用

  • python time与datetime模块基本使用

    time模块基本使用 datetime模块基本使用

  • SQL语句基本使用

    SQL语句基本使用——增删改查 SQL语句基本使用——WHERE子句 SQL语句基本使用——AND和OR的使用 S...

网友评论

      本文标题:Retrofit--->基本使用

      本文链接:https://www.haomeiwen.com/subject/sbseuftx.html