所以我正在玩API的。我正在尝试使用这个API https://rickandmortyapi.com/documentation/#character-schema
我可以调用list 来访问这个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"
}
]
get all characters调用
出现问题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类。假设你命名它为AllResponse
data class AllResponse(
val info: Info,
val characters: List<Characters>
)
在你的服务类
@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类方面是正确的。但是既然你用的是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();
模型类
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;
}
}