如何使用 Retrofit 和 Gson 获取外键字段?

How do I get a foreign key's field using Retrofit and Gson?

我有一个 api 端点输出此 json 事件 table

{
    "name": "",
    "time": null,
    "event_pic_url": null,
    "description": "",
    "event_type": null,
    "invite_only": false,
    "free": false,
    "age_restriction": false,
    "ticket_price": null,
    "venue": null
}

场地字段是具有此格式的场地 table 的外键。

{
    "name": "",
    "rating": null,
    "longitude": null,
    "latitude": null
}

得到事件列表后,我想把它们放在recyclerview上(我已经可以得到列表并知道如何使用适配器)但我不想显示场地的{id},我想使用场地的{name}。我该怎么做呢?它与嵌套 json 反序列化的方式有关吗?

在所有的评论之后,我假设你现在有这样的东西:

{
  "name": "",
  "time": null,
  "event_pic_url": null,
  "description": "",
  "event_type": null,
  "invite_only": false,
  "free": false,
  "age_restriction": false,
  "ticket_price": null,
  "venue": {
     "name": "",
     "rating": null,
     "longitude": null,
     "latitude": null
  }
}

由于您使用的是 Gson,因此您需要以下型号

public class Venue {

  @SerializedName("name")
  @Expose
  private String name;
  @SerializedName("rating")
  @Expose
  private Integer rating;
  @SerializedName("longitude")
  @Expose
  private Double longitude;
  @SerializedName("latitude")
  @Expose
  private Double latitude;

  // ...
}

public class Event {

  @SerializedName("name")
  @Expose
  private String name;
  @SerializedName("time")
  @Expose
  private String time;
  @SerializedName("event_pic_url")
  @Expose
  private String eventPicUrl;
  @SerializedName("description")
  @Expose
  private String description;
  @SerializedName("event_type")
  @Expose
  private String eventType;
  @SerializedName("invite_only")
  @Expose
  private Boolean inviteOnly;
  @SerializedName("free")
  @Expose
  private Boolean free;
  @SerializedName("age_restriction")
  @Expose
  private Boolean ageRestriction;
  @SerializedName("ticket_price")
  @Expose
  private Double ticketPrice;
  @SerializedName("venue")
  @Expose
  private Venue venue;
  // ...
}

请注意,我在这里假设了一些数据类型,即 latitudelongitude 以及 event_type。因为在 json 中它们是 null 我真的不能确定,但​​我想你可以从这个例子中理解。另外请添加适当的 getter 和 setter。

我希望您专注于 venue 部分。如您所见,我基本上是在 Java 对象中重新创建 "nested" json 部分。仅此而已,Gsonretrofit 将为您完成剩下的工作。这是如何做。提醒一句——这可能会因你做事的方式而有很大差异。我更喜欢 rxjava,但我会在这里使用回调方法,因为它更容易解释。

改造 1.9 你可以这样做:

public interface EventService {
   @GET("/url/to/events/endpoint/")
   public void get(Callback<Event> callback);
}

如果一切顺利,在回调的 success 方法上,您将获得 Event 的实例,您可以在其中访问 Venue 对象,前提是返回 json其实就是上面那个

Retrofit 2 界面有点变化,但本质上还是和以前一样的思路:

public interface EventService {
   @GET("/url/to/events/endpoint/")
   public Call<Event> get();
}

一旦您 enqueue 请求并定义了 Callback 对象,您还将在成功方法中获得一个 Event 对象,该对象将引用一个地点。以下是这些回调可能如何通过 Retrofit 2 实现(可能在 retrofit 版本之间略有不同。我不完全记得):

eventService.get().enqueue(new Callback<Event>() {
  @Override public void onResponse(Call<Event> call, Response<Event> response) {
    if (!response.isSuccessful()) {
      // Handle http error
      return;
    }

    Event event = response.body();
    Venue venue = event.getVenue();

    // do something with it
  }

  @Override public void onFailure(Call<Event> call, Throwable t) {
    // Handle error
  }
 });
}

这里eventServiceRetrofit.create(EventService.class)创建的对象。

同样,改装钻头可能会根据您要使用的方法而改变。重要的是要了解如何从 json 响应映射到 java 对象,基本上您只需要复制相同的 json 结构,但在 java 对象中。希望对你有帮助。