无法在Android Retrofit库中为我的课程创build转换器

Im从使用Volley迁移到Retrofit,我已经有了用于将JSONObject响应转换为实现gson注释的对象的gson类。 当我试图使用改进http请求请求,但然后我的应用程序崩溃与此错误:

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 

我遵循在改造网站的指导,我想出了这些实现:

这是我正在尝试执行复古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); } 

最后,应该是反应的宠物类:

 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]; } }; } 

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

默认情况下,Retrofit只能将HTTP主体反序列化为OkHttp的ResponseBodytypes,并且只能接受其@Body RequestBodytypes。

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进行扩展。

如果将来有人遇到这种情况,因为您正在尝试定义自己的自定义转换工厂,并且正在收到此错误,则也可能是由于在具有相同序列化名称的类中有多个variables而导致的。 IE:

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

序列化名称定义了两次(可能是错误)也会抛出这个完全相同的错误。

更新 :请记住,这个逻辑也通过inheritance成立。 如果使用与子类中相同的序列化名称的对象扩展到父类,则会导致相同的问题。

根据最新的评论,我更新了我的import

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

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

在我的情况下,我的模态类内有一个TextView对象,GSON不知道如何序列化它。 标记为“短暂”解决了这个问题。