如何访问此 Api?

How to access this Api?

所以我在玩 API。我正在尝试使用这个 API https://rickandmortyapi.com/documentation/#character-schema

我可以访问这个调用列表

https://rickandmortyapi.com/api/character/1,183
[
  {
    "id": 1,
    "name": "Rick Sanchez",
    "status": "Alive",
    "species": "Human",
    "type": "",
    "gender": "Male",
    "origin": {
      "name": "Earth (C-137)",
      "url": "https://rickandmortyapi.com/api/location/1"
    },
    "location": {
      "name": "Earth (Replacement Dimension)",
      "url": "https://rickandmortyapi.com/api/location/20"
    },
    "image": "https://rickandmortyapi.com/api/character/avatar/1.jpeg",
    "episode": [
      "https://rickandmortyapi.com/api/episode/1",
      "https://rickandmortyapi.com/api/episode/2",
      // ...
    ],
    "url": "https://rickandmortyapi.com/api/character/1",
    "created": "2017-11-04T18:48:46.250Z"
  },
  {
    "id": 183,
    "name": "Johnny Depp",
    "status": "Alive",
    "species": "Human",
    "type": "",
    "gender": "Male",
    "origin": {
      "name": "Earth (C-500A)",
      "url": "https://rickandmortyapi.com/api/location/23"
    },
    "location": {
      "name": "Earth (C-500A)",
      "url": "https://rickandmortyapi.com/api/location/23"
    },
    "image": "https://rickandmortyapi.com/api/character/avatar/183.jpeg",
    "episode": [
      "https://rickandmortyapi.com/api/episode/8"
    ],
    "url": "https://rickandmortyapi.com/api/character/183",
    "created": "2017-12-29T18:51:29.693Z"
  }
]

获取所有字符调用时遇到问题

https://rickandmortyapi.com/api/character
{
  "info": {
    "count": 671,
    "pages": 34,
    "next": "https://rickandmortyapi.com/api/character/?page=2",
    "prev": null
  },
  "results": [
    {
      "id": 1,
      "name": "Rick Sanchez",
      "status": "Alive",
      "species": "Human",
      "type": "",
      "gender": "Male",
      "origin": {
        "name": "Earth",
        "url": "https://rickandmortyapi.com/api/location/1"
      },
      "location": {
        "name": "Earth",
        "url": "https://rickandmortyapi.com/api/location/20"
      },
      "image": "https://rickandmortyapi.com/api/character/avatar/1.jpeg",
      "episode": [
        "https://rickandmortyapi.com/api/episode/1",
        "https://rickandmortyapi.com/api/episode/2",
        // ...
      ],
      "url": "https://rickandmortyapi.com/api/character/1",
      "created": "2017-11-04T18:48:46.250Z"
    },
    // ...
  ]
}

我是这样称呼它的

    @GET("character")
    suspend fun getAll() : Response<Characters>
}

我得到的只有这个

响应{protocol=h2, code=200, message=, url=https://rickandmortyapi.com/api/character}

如何从获取所有字符端点访问“结果”列表? 还在这学习。我已经掌握了基本的 API,但是这一项超出了我的理解范围。

这是我使用的插件生成的 类。

data class Characters(
    val created: String,
    val episode: List<Any>,
    val gender: String,
    val id: Int,
    val image: String,
    val location: Location,
    val name: String,
    val origin: Origin,
    val species: String,
    val status: String,
    val type: String,
    val url: String
)

data class Location(
    val name: String,
    val url: String
)

data class Origin(
    val name: String,
    val url: String
)

您将需要另一个 POJO class。假设您将其命名为 AllResponse

data class AllResponse(
    val info: Info,
    val characters: List<Characters>
)

为您服务class

    @GET("character")
    suspend fun getAll() : Response<AllResponse>

访问字符列表

val call = myService.getAll()
call.enqueue(object : Callback<AllResponse> {
    override fun onResponse(call: Call<AllResponse>, response: Response<AllResponse>) {
        if (response.isSuccessful) {
            val allResponse = response.body()
            val characters = allResponse.characters
        }
    }
    override fun onFailure(call: Call<Characters>, t: Throwable) {

    }
})

我认为 Divij Gupta 在 POJO 方面是正确的 class。但是由于您使用的是 suspend。我认为您需要一个协程来从 api.

获取结果
val call. myService.getAll()

if(call.isSuccessful) {  <-- coroutines's way to get result in sequential.
   call.body()?.let {
       val characters = it.characters
   }
} else {
   // handle error here
}

PS:您可以考虑将它们放入存储库并将值放入您的 viewModel。

   @FormUrlEncoded
    @POST("xyz")
    Call<SamplePojo> getData(
            @Field("xyx") String xyz,
           );

    @GET("xyz")
    Call<SamplePojo> getData();

型号class

package com.sample.demo;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Location {

@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}
-----------------------------------com.sample.demo.Origin.java-----------------------------------

package com.sample.demo;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Origin {

@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}
-----------------------------------com.sample.demo.SamplePojo.java-----------------------------------

package com.sample.demo;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class SamplePojo {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("status")
@Expose
private String status;
@SerializedName("species")
@Expose
private String species;
@SerializedName("type")
@Expose
private String type;
@SerializedName("gender")
@Expose
private String gender;
@SerializedName("origin")
@Expose
private Origin origin;
@SerializedName("location")
@Expose
private Location location;
@SerializedName("image")
@Expose
private String image;
@SerializedName("episode")
@Expose
private List<String> episode = null;
@SerializedName("url")
@Expose
private String url;
@SerializedName("created")
@Expose
private String created;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getSpecies() {
return species;
}

public void setSpecies(String species) {
this.species = species;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public Origin getOrigin() {
return origin;
}

public void setOrigin(Origin origin) {
this.origin = origin;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public List<String> getEpisode() {
return episode;
}

public void setEpisode(List<String> episode) {
this.episode = episode;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getCreated() {
return created;
}

public void setCreated(String created) {
this.created = created;
}

}