本文介绍了改造 2 beta-4.Android 6. 无法创建转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决?

Process: www.palchiki.com.palchiki, PID: 2047
java.lang.IllegalArgumentException: Unable to create converter for class www.palchiki.com.palchiki.model.ServiceResponse
for method APIWebService.getServices
at retrofit2.Utils.methodError(Utils.java:154)
at retrofit2.MethodHandler.createResponseConverter(MethodHandler.java:62)
at retrofit2.MethodHandler.create(MethodHandler.java:33)
at retrofit2.Retrofit.loadMethodHandler(Retrofit.java:164)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy2.getServices(Unknown Source)
at www.palchiki.com.palchiki.fragment.ServiceFragment.onCreateView(ServiceFragment.java:94)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)

我使用 com.squareup.retrofit2:retrofit:2.0.0-beta4 和 com.squareup.retrofit2:converter-gson:2.0.0-beta4

I use com.squareup.retrofit2:retrofit:2.0.0-beta4 and com.squareup.retrofit2:converter-gson:2.0.0-beta4

适配器

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(URLWebService)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();

界面

@GET("/integration.php?command=getServices")
Call<ServiceResponse> getServices(@Query("city") String cityId);

推荐答案

您必须为 ServiceResponse 类指定一个转换器,以便 gson 有效地序列化/反序列化您的类它会像这样

You have to specify a Converter for the class ServiceResponse in order for gson to serialize/deserialize your class effectivelyit would go like this

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(ServiceResponse.class, new ServiceResponseJson());
Gson gson = builder.build();

稍后,您必须像这样将这个 gson 实例提供给您的 gson 转换器工厂

later on, you ll have to provide this gson instance to your gson converter factory like this

....addConverterFactory(GsonConverterFactory.create(gson))

ServiceResponseJson 必须实现JsonDeserializer 和/或 JsonSerializer

ServiceResponseJson will have to implementJsonDeserializer and/or JsonSerializer

这篇关于改造 2 beta-4.Android 6. 无法创建转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:25