如何在Retrofit 2中使用AutoValue? [英] How to use AutoValue with Retrofit 2?

查看:73
本文介绍了如何在Retrofit 2中使用AutoValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在项目中使用了AutoValue(和android-apt插件),并且我知道Ryan Harter对AutoValue的gson扩展,但是我该如何挂钩Retrofit 2以在其上使用扩展和工厂方法抽象类?

  String grantType ="password";呼叫< SignIn>signInCall = retrofitApi.signIn(电子邮件,密码,grantType);signInCall.enqueue(回调); 

例如,在这里,我想将AutoValue与SignIn JSON模型对象一起使用以实现不可变性,但是我该如何将Retrofit(或者更具体地说是Gson)连接到不可变的AutoValue模型类?

解决方案

[更新]库有所更改,请在此处查看更多信息:

如何创建和使用实时模板.

I've got AutoValue (and the android-apt plugin) working in a project, and I'm aware of Ryan Harter's gson extension for AutoValue, but how do I hook Retrofit 2 up to use the extension and factory method on the abstract class?

String grantType = "password";
Call<SignIn> signInCall = retrofitApi.signIn(email, password, grantType);
signInCall.enqueue(callback);

eg here I would like to use AutoValue with the SignIn JSON model object to enforce immutability but how do I hook up Retrofit (or perhaps more specifically Gson) to the immutable, AutoValue model class?

解决方案

[update] The library have changed a bit, check more here: https://github.com/rharter/auto-value-gson

I was able to make it work like this. I hope it will help you.

  • Import in your gradle app file

    apt 'com.ryanharter.auto.value:auto-value-gson:0.3.1'

  • Create object with autovalue:

    @AutoValue public abstract class SignIn {    
        @SerializedName("signin_token") public abstract String signinToken();
        @SerializedName("user") public abstract Profile profile();
    
        public static TypeAdapter<SignIn> typeAdapter(Gson gson) {
            return new AutoValue_SignIn.GsonTypeAdapter(gson);
        }
    }
    

  • Create your Type Adapter Factory (Skip if using version > 0.3.0)

    public class AutoValueGsonTypeAdapterFactory implements TypeAdapterFactory {
    
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            Class<? super T> rawType = type.getRawType();
    
            if (rawType.equals(SignIn.class)) {
                return (TypeAdapter<T>) SignIn.typeAdapter(gson);
            } 
    
            return null;
        }
    }
    

  • Create your Gson converter with your GsonBuilder

    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(
            new GsonBuilder()
                    .registerTypeAdapterFactory(new AutoValueGsonTypeAdapterFactory())
                    .create());
    

  • Add it to your retrofit builder

    Retrofit retrofit = new Retrofit
            .Builder()
            .addConverterFactory(gsonConverterFactory)
            .baseUrl("http://url.com/")
            .build()
    

  • Do your request

  • Enjoy

Bonus live template:
In your autovalue class, type avtypeadapter then autocomplete to generate the type adapter code. To work you need to add this as a live template in Android Studio.

public static TypeAdapter<$class$> typeAdapter(Gson gson) {
    return new AutoValue_$class$.GsonTypeAdapter(gson);
}

How to create and use the live template.

这篇关于如何在Retrofit 2中使用AutoValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆