进行以下模型类(parcelable)
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example implements Parcelable {
@SerializedName("bgroup")
@Expose
private String bgroup;
public String getBgroup() {
return bgroup;
}
public void setBgroup(String bgroup) {
this.bgroup = bgroup;
}
protected Example(Parcel in) {
bgroup = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(bgroup);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Example> CREATOR = new Parcelable.Creator<Example>() {
@Override
public Example createFromParcel(Parcel in) {
return new Example(in);
}
@Override
public Example[] newArray(int size) {
return new Example[size];
}
};
}
不是创建接口类这样
public interface ApiService {
@GET("<URL>")
Call<List<Example>> getBloodGroups();
}
最后调用改造喜欢以下内容:
Call<List<Example>> call = new RestClient(this).getApiService()
.getBloodGroups();
call.enqueue(new Callback<List<Example>>() {
@Override
public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
}
@Override
public void onFailure(Call<List<Example>> call, Throwable throwable) {
}
});
它清楚地告诉你,它无法从对象来自哪里读取字符串。为什么不创建一个映射来让'List'后来直接从'Mapping.bgroup'提取一个字符串呢? –
如果您为自定义数据对象使用了相同的字符串,则自动翻新会自动为您提供带有值的格式化数据对象。所以使用相同的密钥。并使用依赖编译'com.squareup.retrofit2:converter-gson:2.0.2' –
@LyubomyrShaydariv感谢您的回答。你可以给这个场景简单的列表例子吗? –