美文网首页
★43.Retrofit + Gson

★43.Retrofit + Gson

作者: iDragonfly | 来源:发表于2017-06-29 00:08 被阅读0次

简介

  • Gson Converter官网
  • 注意到 Retrofit 所有的接口定义都是返回Call<ResponseBody>,但是其实可以是别的类型,比如Call<Blog>,这需要相应的Converter

反序列化示例(Json字符串->Model对象)

1. 定义Model

public interface BlogService {
    @GET("blog/{id}")
    Call<Blog> getFirstBlog(@Path("id") int id);
}

2. 创建Gson

Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd hh:mm:ss")
        .create();

3. 创建Retrofit

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://localhost:4567/")
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

4. 创建Model

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

5. 创建Call

Call<Blog> call = service.getBlog(2);

6. 执行Call

call.enqueue(new Callback<Blog>() {
    @Override
    public void onResponse(Call<Blog> call, Response<Blog> response) {
        // 已经转换为想要的类型了
        Blog result = response.body();
        System.out.println(result);
    }

    @Override
    public void onFailure(Call<Blog> call, Throwable t) {
        t.printStackTrace();
    }
});

序列化示例(Model对象->Json字符串)

1. 定义Model

public interface BlogService {
    @POST("blog")
    Call<Blog> createBlog(@Body Blog blog);
}

2. 构建Gson

Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd hh:mm:ss")
        .create();

3. 构建Retrofit

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://localhost:4567/")
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

4. 创建Model

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

5. 创建Call

Blog blog = new Blog();
blog.content = "新建的Blog";
blog.title = "测试";
blog.author = "name";
Call<Blog> call = service.createBlog(blog);

6. 执行Call

call.enqueue(new Callback<Blog>() {
    @Override
    public void onResponse(Call<Blog> call, Response<Blog> response) {
        // 已经转换为想要的类型了
        Blog result = response.body();
        System.out.println(result);
    }

    @Override
    public void onFailure(Call<Blog> call, Throwable t) {
        t.printStackTrace();
    }
});

相关文章

  • ★43.Retrofit + Gson

    简介 Gson Converter官网。 注意到 Retrofit 所有的接口定义都是返回Call

  • Gson教程 Apache POI教程 Guava教程Apac

    Gson教程 Gson概述Gson环境设置Gson第一个应用Gson classGson对象序列化Gson数据绑定...

  • list集合转换为json

    //后台Gson gson=new Gson();String json=gson.toJson(集合, new ...

  • GSON 解析 JSON

    GSON JSON 介绍 Gson 下载 Gson 解析 和 格式化Gson 格式化Gson 解析 解析asset...

  • 2018-01-11

    Gson解析复杂json数据常用的两种解析方式 Gson gson = new Gson(); 1.gson.fr...

  • Gson的使用

    序列化对象: Gson gson = new Gson();String json = gson.toJson(o...

  • Tools

    完全理解Gson(3):Gson反序列化 完全理解Gson(2):Gson序列化 完全理解Gson(1):简单入门...

  • 使用Gson将=转成Json对象时出现\u003d的问题

    Gson将对象转成Json对象的方法 改之前: Gson gson=new Gson(); String json...

  • Android 库 Gson

    【Android 库 Gson】 引用: ★Gson 解析教程★★★ Gson的入门使用Gson全解析(上)-Gs...

  • java gson的使用

    1、依赖jar包 gson-2.7.jar 2、调用方法: Gson gson = new Gson();Stri...

网友评论

      本文标题:★43.Retrofit + Gson

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