Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何通过Retrofit在@Query中传递自定义枚举?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的枚举:
public enum Season {
    @serializedName("0")
    AUTUMN,@serializedName("1")
    SPRING;
}

从某个版本开始,GSON就能够解析这些枚举.为了确保,我这样做了

final @R_675_10495@ng s = gson.toJson(Season.AUTUMN);

它按我的预期工作.输出为“0”.所以,我尝试在我的Retrofit服务中使用它:

@GET("index.PHP?page[api]=test")
Observable<List<Month>> getMonths(@Query("season_lookup") Season season);
/*..@L_398_4@me files later...*/
service.getMonths(Season.AUTUMN);

并且还添加了日志以确定其结果:

httpLoggingInterceptor httpLoggingInterceptor = new httpLoggingInterceptor();
httpLoggingInterceptor.setLevel(httpLoggingInterceptor.Level.bODY);

OkhttpClient httpClient = new OkhttpClient.builder()
        .addInterceptor(httpLoggingInterceptor)
        .build();

但它失败了. @Query完全忽略@serializedName并改为使用.to@R_675_10495@ng(),所以日志显示为… / index.PHP?page [api] = test& season_lookup = AUTUMN.

我跟踪了Retrofit源代码并找到了带有行的文件requestFactoryParser:

Converter<?,@R_675_10495@ng> converter = 
    retrofit.@R_675_10495@ngConverter(parameterType,parameterAnnotations);
action = new requestAction.Query<>(name,converter,encoded);

似乎,就像它根本不关心枚举一样.在这些行之前,它将rawParameterType.isArray()测试为数组或Iterable.class.isAssignableFrom(),仅此而已.

改造实例创建是:

retrofit = new Retrofit.builder()
                .baseUrl(ApiConstants.API_ENDPOint)
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

gson是GsonBuilder().create().我偷看了源代码,其中有枚举的预定义ENUM_TypeAdapters.ENUM_FACTORY,所以我保持原样.

问题是我该怎么办,以防止在我的枚举上使用to@R_675_10495@ng()并使用@serializedName?我将to@R_675_10495@ng()用于其他目的.

解决方法

@H_675_35@ 正如@DawidSzydło所提到的,我在Retrofit中误解了Gson的用法.它仅用于响应/请求解码/编码,但不用于@Query / @ Url / @ Path e.t.c.对于他们来说,Retrofit使用COnverter.Factory将任何类型转换为@R_675_10495@ng.
以下是将@serializedName作为任何Enum的值传递给Retrofit服务时自动使用的代码.

转换器:

public class EnumRetrofitConverterFactory extends Converter.Factory {
    @Override
    public Converter<?,@R_675_10495@ng> @R_675_10495@ngConverter(Type type,Annotation[] Annotations,Retrofit retrofit) {
        Converter<?,@R_675_10495@ng> converter = null;
        if (type instanceof Class && ((Class<?>)typE).isEnum()) {
            converter = value -> EnumUtils.GetserializedNameValue((Enum) value);
        }
        return converter;
    }
}

EnumUtils:

public class EnumUtils {
    @Nullable
    static public <E extends Enum<E>> @R_675_10495@ng GetserializedNameValue(E E) {
        @R_675_10495@ng value = null;
        try {
            value = e.getClass().getField(e.name()).getAnnotation(serializedName.class).value();
        } catch (NoSuchFieldException exception) {
            exception.printStackTrace();
        }
        return value;
    }
}

改造创造:

retrofit = new Retrofit.builder()
        .baseUrl(ApiConstants.API_ENDPOint)
        .client(httpClient)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .addConverterFactory(new EnumRetrofitConverterFactory())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build();

08.18更新添加kotlin类似物:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceStatE)
        setContentView(R.layout.activity_main)

        val httpLoggingInterceptor = httpLoggingInterceptor()
        httpLoggingInterceptor.level = httpLoggingInterceptor.Level.bODY

        val httpClient = OkhttpClient.builder()
                .addInterceptor(httpLoggingInterceptor)
                .build()

        val gson = GsonBuilder().create()

        val retrofit = Retrofit.builder()
                .baseUrl(Api.ENDPOint)
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addConverterFactory(EnumConverterFactory())
                .build()

        val service = retrofit.create(Api::class.java)
        service.getMonths(Season.AUTUMN).enqueue(object : CallBACk<List<@R_675_10495@ng>> {
            override fun onFailure(call: Call<List<@R_675_10495@ng>>?,t: Throwable?) {
                /* ignore */
            }

            override fun onResponse(call: Call<List<@R_675_10495@ng>>?,response: Response<List<@R_675_10495@ng>>?) {
                /* ignore */
            }
        })
    }
}

class EnumConverterFactory : Converter.Factory() {
    override fun @R_675_10495@ngConverter(type: Type?,Annotations: Array<out Annotation>?,retrofit: Retrofit?): Converter<*,@R_675_10495@ng>? {
        if (type is Class<*> && type.isEnum) {
            return Converter<Any?,@R_675_10495@ng> { value -> getserializedNameValue(value as Enum<*>) }
        }
        return null
    }
}

fun <E : Enum<*>> getserializedNameValue(e: E): @R_675_10495@ng {
    try {
        return e.javaClass.getField(e.Name).getAnnotation(serializedName::class.java).value
    } catch (exception: NoSuchFieldException) {
        exception.printStackTrace()
    }

    return ""
}

enum class Season {
    @serializedName("0")
    AUTUMN,@serializedName("1")
    SPRING
}

interface Api {
    @GET("index.PHP?page[api]=test")
    fun getMonths(@Query("season_lookup") season: Season): Call<List<@R_675_10495@ng>>

    companion object {
        const val ENDPOINT = "http://127.0.0.1"
    }
}

在日志中,您将看到:

D/Okhttp: --> GET http://127.0.0.1/index.PHP?page[api]=test&season_lookup=0 
D/Okhttp: --> END GET 
D/Okhttp: <-- http Failed: java.net.ConnectException: Failed to connect to /127.0.0.1:80

使用的依赖项是:

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

大佬总结

以上是大佬教程为你收集整理的android – 如何通过Retrofit在@Query中传递自定义枚举?全部内容,希望文章能够帮你解决android – 如何通过Retrofit在@Query中传递自定义枚举?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。