无法在Android Retrofit库中为我的课程创建转换器 [英] Unable to create converter for my class in Android Retrofit library
问题描述
无法启动活动ComponentInfo {com.lightbulb.pawesome / com.example.sample.retrofit.SampleActivity}:java.lang.IllegalArgumentException:无法为类com.lightbulb.pawesome.model.Pet创建转换器
为方法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);
呼叫< 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() );
}
});
尝试{
callPet.execute();
} catch(IOException e){
e.printStackTrace();
}
}
}
我的界面它变成了我的API
public interface GitHubService {
@GET(/ ** sample here ** / {petId} / {otherPet})
呼叫< Pet> getResponse(@Path(petId)String userId,@Path(otherPet)String otherPet);
}
最后,Pet类应该是响应者:
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
私人字符串名称;
@Expose
私人字符串性别;
@Expose
私人字符串年龄;
@Expose
私人字符串品种;
@SerializedName(profile_picture)
@Expose
private String profilePicture;
@SerializedName(confirmation_status)
@Expose
private String confirmationStatus;
$ b $ **
*
* @return
* confirmationStatus
* /
public String getConfirmationStatus(){
返回confirmationStatus;
}
/ **
*
* @param confirmationStatus
* confirmation_status
* /
public void setConfirmationStatus(字符串confirmationStatus){
this.confirmationStatus = confirmationStatus;
$ b / **
*
* @return
* isActive
* /
public String getIsActive() {
return isActive;
}
/ **
*
* @param isActive
* is_active
* /
public void setIsActive字符串isActive){
this.isActive = isActive;
$ b / **
*
* @return
* petId
* /
public String getPetId() {
return petId;
}
/ **
*
* @param petId
* pet_id
* /
public void setPetId字符串petId){
this.petId = petId;
$ b / **
*
* @return
*名称
* /
public String getName() {
返回姓名;
$ b / **
*
* @param名称
*名称
* /
public void setName(字符串名称){
this.name = name;
$ b / **
*
* @return
*性别
* /
public String getGender() {
返回性别;
}
/ **
*
* @param gender
*性别
* /
public void setGender字符串性别){
this.gender = gender;
$ b / **
*
* @return
*年龄
* /
public String getAge() {
回归年龄;
}
/ **
*
* @参数年龄
*年龄
* /
public void setAge字符串年龄){
this.age =年龄;
$ b / **
*
* @return
*品种
* /
public String getBreed() {
返回品种;
}
/ **
*
* @param繁殖
*繁殖
* /
public void setBreed字符串品种){
this.breed =品种;
$ b / **
*
* @return
* profilePicture
* /
public String getProfilePicture() {
return profilePicture;
}
$ b $ **
*
* @param profilePicture
* profile_picture
* /
public void setProfilePicture( String profilePicture){
this.profilePicture = profilePicture;
保护宠物(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;
$ b @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];
}
};
c> 2.0.0 ,默认的转换器是一个gson转换器,但是在 2.0.0
中,后来默认的转换器是 ResponseBody
。从文档:
默认情况下,Retrofit只能将HTTP正文序列化为OkHttp的
code>类型,它只能接受它的
ResponseBody
RequestBody
类型为
@Body
。在$2.0.0 +
中,你需要明确指定你想要一个Gson转换器:改造retrofit = new Retrofit.Builder()
.baseUrl(** sample base url here ** )
.addConverterFactory(GsonConverterFactory.create())
.build();
您还需要将以下依赖项添加到您的gradle文件中:
compile'com.squareup.retrofit2:converter-gson:2.1.0'
使用与您的改造相同的版本。以上符合这种改造依赖:
$ p $compile('com.squareup.retrofit2:retrofit:2.1.0')
另外,请注意,在撰写本文时,翻新文档并未完全更新,这就是为什么该示例让您深入了解麻烦。从文档:
注意:该网站仍在为新的2.0 API进行扩展。
Im migrating from using Volley to Retrofit, I already have gson class that I used before for converting JSONObject reponse to a object that implements gson annotations. When I'm trying to make http get request using retrofit but then my app crashes with this error :
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
Im following the guide in retrofit site and I come up with these implementations :
This is my activity where I am trying to execute the retro http request:
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(); } } }
My interface which turned to be my API
public interface GitHubService { @GET("/ **sample here** /{petId}/{otherPet}") Call<Pet> getResponse(@Path("petId") String userId, @Path("otherPet") String otherPet); }
And finally the Pet class which should be the reponse:
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]; } }; }
解决方案Prior to
2.0.0
, the default converter was a gson converter, but in2.0.0
and later the default converter isResponseBody
. From the docs:By default, Retrofit can only deserialize HTTP bodies into OkHttp's
ResponseBody
type and it can only accept itsRequestBody
type for@Body
.In
2.0.0+
, you need to explicitly specify you want a Gson converter:Retrofit retrofit = new Retrofit.Builder() .baseUrl("**sample base url here**") .addConverterFactory(GsonConverterFactory.create()) .build();
You will also need to add the following dependency to your gradle file:
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Use the same version for the converter as you do for your retrofit. The above matches this retrofit dependency:
compile ('com.squareup.retrofit2:retrofit:2.1.0')
Also, note as of writing this, the retrofit docs are not completely updated, which is why that example got you into trouble. From the docs:
Note: This site is still in the process of being expanded for the new 2.0 APIs.
这篇关于无法在Android Retrofit库中为我的课程创建转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!