繁体   English   中英

无法在 Android Retrofit 库中为我的 class 创建转换器

[英]Unable to create converter for my class in Android Retrofit library

我从使用 Volley 迁移到 Retrofit,我已经有 gson class,我之前使用它来将 JSONObject 响应转换为实现 gson 注释的 object。 当我尝试使用 retrofit 发出 http get 请求时,我的应用程序崩溃并出现此错误:

 Unable to start activity ComponentInfo{com.lightbulb.pawesome/com.example.sample.retrofit.SampleActivity}: java.lang.IllegalArgumentException: Unable to create converter for class com.lightbulb.pawesome.model.Pet
    for method GitHubService.getResponse

我按照retrofit站点中的指南进行操作,并提出了这些实现:

这是我尝试执行复古 http 请求的活动:

public class SampleActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("**sample base url here**")
                .build();

        GitHubService service = retrofit.create(GitHubService.class);
        Call<Pet> callPet = service.getResponse("41", "40");
        callPet.enqueue(new Callback<Pet>() {
            @Override
            public void onResponse(Response<Pet> response) {
                Log.i("Response", response.toString());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.i("Failure", t.toString());
            }
        });
        try{
            callPet.execute();
        } catch (IOException e){
            e.printStackTrace();
        }

    }
}

我的界面变成了我的 API

public interface GitHubService {
    @GET("/ **sample here** /{petId}/{otherPet}")
    Call<Pet> getResponse(@Path("petId") String userId, @Path("otherPet") String otherPet);
}

最后是 Pet class,它应该是响应:

public class Pet implements Parcelable {

    public static final String ACTIVE = "1";
    public static final String NOT_ACTIVE = "0";

    @SerializedName("is_active")
    @Expose
    private String isActive;
    @SerializedName("pet_id")
    @Expose
    private String petId;
    @Expose
    private String name;
    @Expose
    private String gender;
    @Expose
    private String age;
    @Expose
    private String breed;
    @SerializedName("profile_picture")
    @Expose
    private String profilePicture;
    @SerializedName("confirmation_status")
    @Expose
    private String confirmationStatus;

    /**
     *
     * @return
     * The confirmationStatus
     */
    public String getConfirmationStatus() {
        return confirmationStatus;
    }

    /**
     *
     * @param confirmationStatus
     * The confirmation_status
     */
    public void setConfirmationStatus(String confirmationStatus) {
        this.confirmationStatus = confirmationStatus;
    }

    /**
     *
     * @return
     * The isActive
     */
    public String getIsActive() {
        return isActive;
    }

    /**
     *
     * @param isActive
     * The is_active
     */
    public void setIsActive(String isActive) {
        this.isActive = isActive;
    }

    /**
     *
     * @return
     * The petId
     */
    public String getPetId() {
        return petId;
    }

    /**
     *
     * @param petId
     * The pet_id
     */
    public void setPetId(String petId) {
        this.petId = petId;
    }

    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     * The name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     * The gender
     */
    public String getGender() {
        return gender;
    }

    /**
     *
     * @param gender
     * The gender
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     *
     * @return
     * The age
     */
    public String getAge() {
        return age;
    }

    /**
     *
     * @param age
     * The age
     */
    public void setAge(String age) {
        this.age = age;
    }

    /**
     *
     * @return
     * The breed
     */
    public String getBreed() {
        return breed;
    }

    /**
     *
     * @param breed
     * The breed
     */
    public void setBreed(String breed) {
        this.breed = breed;
    }

    /**
     *
     * @return
     * The profilePicture
     */
    public String getProfilePicture() {
        return profilePicture;
    }

    /**
     *
     * @param profilePicture
     * The profile_picture
     */
    public void setProfilePicture(String profilePicture) {
        this.profilePicture = profilePicture;
    }


    protected Pet(Parcel in) {
        isActive = in.readString();
        petId = in.readString();
        name = in.readString();
        gender = in.readString();
        age = in.readString();
        breed = in.readString();
        profilePicture = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(isActive);
        dest.writeString(petId);
        dest.writeString(name);
        dest.writeString(gender);
        dest.writeString(age);
        dest.writeString(breed);
        dest.writeString(profilePicture);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Pet> CREATOR = new Parcelable.Creator<Pet>() {
        @Override
        public Pet createFromParcel(Parcel in) {
            return new Pet(in);
        }

        @Override
        public Pet[] newArray(int size) {
            return new Pet[size];
        }
    };
}

如果将来有人因为您尝试定义自己的自定义转换器工厂并收到此错误而遇到此问题,也可能是由于类中有多个变量拼写错误或序列化名称相同造成的。 IE:

public class foo {
  @SerializedName("name")
  String firstName;
  @SerializedName("name")
  String lastName;
}

将序列化名称定义两次(可能是错误的)也会引发完全相同的错误。

更新:请记住,此逻辑也适用于继承。 如果您使用与子类中具有相同序列化名称的对象扩展到父类,则会导致同样的问题。

2.0.0之前,默认转换器是 gson 转换器,但在2.0.0及更高版本中,默认转换器是ResponseBody 从文档:

默认情况下,Retrofit 只能将 HTTP 主体反序列化为 OkHttp 的ResponseBody类型,并且它只能接受@Body RequestBody类型。

2.0.0+ ,您需要明确指定您想要一个 Gson 转换器:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("**sample base url here**")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

您还需要将以下依赖项添加到您的 gradle 文件中:

compile 'com.squareup.retrofit2:converter-gson:2.1.0'

对转换器使用与改造时相同的版本。 以上与此改造依赖项相匹配:

compile ('com.squareup.retrofit2:retrofit:2.1.0')

另外,请注意,在撰写本文时,改造文档并未完全更新,这就是该示例让您陷入困境的原因。 从文档:

注意:此站点仍在为新的 2.0 API 进行扩展。

只需确保您没有两次使用相同的序列化名称

 @SerializedName("name") val name: String
 @SerializedName("name") val firstName: String

只需删除其中之一

根据热门评论,我更新了我的导入

implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

我已经使用http://www.jsonschema2pojo.org/从 Spotify json 结果创建 pojo,并确保指定 Gson 格式。

现在有 Android Studio 插件可以为您创建 pojo 或 Kotlin 数据模型。 mac 的一个很好的选择是 Quicktype。 https://itunes.apple.com/us/app/paste-json-as-code-quicktype/id1330801220

就我而言,我的模态类中有一个 TextView 对象,而 GSON 不知道如何序列化它。 将其标记为“瞬态”解决了这个问题。

@Silmarilos 的帖子帮助我解决了这个问题。 就我而言,我使用“id”作为序列化名称,如下所示:

 @SerializedName("id")
var node_id: String? = null

我把它改成

 @SerializedName("node_id")
var node_id: String? = null

现在都在工作。 我忘记了“id”是默认属性。

就我而言,这是由于试图将我的服务返回的 List 放入 ArrayList 中。 所以我所拥有的是:

@Json(name = "items")
private ArrayList<ItemModel> items;

当我应该有

@Json(name = "items")
private List<ItemModel> items;

希望这可以帮助某人!

就我而言,我使用的是 Moshi 库和 Retrofit 2.0,即

// Moshi
implementation 'com.squareup.moshi:moshi-kotlin:1.9.3'
// Retrofit with Moshi Converter
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'

我忘记将自定义 Moshi json 转换器适配器工厂对象传递给 moshi 转换器工厂构造函数。

private val moshi = Moshi.Builder() // adapter
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create()) // <- missing moshi json adapter insance
    .baseUrl(BASE_URL)
    .build()

修复: .addConverterFactory(MoshiConverterFactory.create(moshi))

这可能会帮助某人

在我的情况下,我错误地写了这样的SerializedName

@SerializedName("name","time")
String name,time; 

它应该是

@SerializedName("name")
String name;

@SerializedName("time")
String time;

嘿,我今天遇到了同样的问题,我花了一整天的时间才找到解决方案,但这是我最终找到的解决方案。 我在我的代码中使用了 Dagger,我需要在我的改造实例中实现 Gson 转换器。

所以这是我之前的代码

@Provides
    @Singleton
    Retrofit providesRetrofit(Application application,OkHttpClient client) {
        String SERVER_URL=URL;
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(SERVER_URL);
        return builder
                .client(client)
                .build();
    }

这就是我的结局

@Provides
    @Singleton
    Retrofit providesRetrofit(Application application,OkHttpClient client, Gson gson) {
        String SERVER_URL=URL;
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(SERVER_URL);
        return builder
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }

注意第一个例子中没有转换器,如果你还没有实例化 Gson,你可以像这样添加它

    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();

   gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        return gsonBuilder.create();
    }

并确保您已将其包含在改造的方法调用中。

再次希望这对像我这样的人有所帮助。

就我而言,问题在于我的 SUPERCLASS 模型中定义了该字段。 太蠢了,我知道......

build.gradle改变

minifyEnabled true

minifyEnabled false

解决了我的问题。

就我而言,我将MoshiRetrofit一起使用,我的错误是:

我没有为包含在Response类服务中的对象定义body

例如:

@JsonSerializable
data class Balance(
    @field:Json(name = "balance") var balance: Double,
    @field:Json(name = "currency") var currency: Currency

Currency类是空的。 所以我完成了它并解决了问题!

在我使用kotlinx.serialization的情况下,改造引发了相同的异常,

这是由于缺少@Serializable注释。

@Serializable
data class MyClass(
    val id: String
)

在我的例子中,我缺少Serialization属性。
我必须在每个数据 class 之前添加@kotlinx.serialization.Serializable kotlinx.serialization.Serializable:

@kotlinx.serialization.Serializable
data class RadioSearchPodcastDto(
    val playables: List<Playable>,
    val searchTag: SearchTag,
    val totalCount: Int
)

Retrofit接口:

interface PodcastRadioApi {
     @GET("/podcasts/search")
     suspend fun getPodcastBySearch(@Query("query") query: String,
                                    @Query("count") count: Int,
                                    @Query("offset") offset: Int,
                                    @Query("partner") partner: String): RadioSearchPodcastDto
}

对于每一个,我指的是主要的 class 和所有子类(Playable、SearchTag 等)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM