为了账号安全,请及时绑定邮箱和手机立即绑定

使用改造从 api 获取响应以在 recycleview 中获取响应时出错

使用改造从 api 获取响应以在 recycleview 中获取响应时出错

缥缈止盈 2021-11-17 15:40:39
我正在尝试使用此 api 联网:https : //api.jikan.moe/v3/schedule所以我通过创建 ApiClient.java 类来使用改造,这是它的代码:public class ApiClient {public static final String BASE_URL = "http://api.themoviedb.org/3/";private static Retrofit retrofit = null;public static Retrofit getClient() {    if (retrofit==null) {        retrofit = new Retrofit.Builder()                .baseUrl(BASE_URL)                .addConverterFactory(GsonConverterFactory.create())                .build();    }    return retrofit;}}完整的链接端点的接口是这样的:public interface ApiInterface {    @GET("schedule")    Call<MainResponse> getSchedule();}所以我在建模数据中使用了可序列化,并创建了 MainResponse.java 类来获取 api 中的主要 monday 数组:公共类 MainResponse {@SerializedName("monday")private List<Schedule> monday;public List<Schedule> getMonday() {    return monday;}public void setMonday(List<Schedule> monday) {    this.monday = monday;}}然后我创建一个新的建模类来获取类 Schedule.java 中 monday 数组的对象项列表:public class Schedule {    @SerializedName("title")    private String title;    public Schedule(String title) {        this.title = title;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }}最后在 MainActivity 我称之为:final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.schedule_recycler_view);        recyclerView.setLayoutManager(new LinearLayoutManager(this));        ApiInterface apiService =                ApiClient.getClient().create(ApiInterface.class);        Call<MainResponse> call = apiService.getSchedule();        call.enqueue(new Callback<MainResponse>() {正如你所看到的,我使用了一个 recycleview 来获取 monday 数组的标题,但问题是当我运行应用程序时,它只是因为这个而崩溃:尝试在空对象引用上调用虚拟方法“java.util.List com.example.user_pc.capstonestage2.MainResponse.getMonday()”
查看完整描述

2 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

您已声明错误的 API URL 是:


public static final String BASE_URL = "https://api.jikan.moe/v3/";

你MainResponse应该是这样的:


public class MainResponse {


@SerializedName("request_hash")

@Expose

private String requestHash;

@SerializedName("request_cached")

@Expose

private Boolean requestCached;

@SerializedName("request_cache_expiry")

@Expose

private Integer requestCacheExpiry;

@SerializedName("monday")

@Expose

private List<Monday> monday = null;


public String getRequestHash() {

return requestHash;

}


public void setRequestHash(String requestHash) {

this.requestHash = requestHash;

}


public Boolean getRequestCached() {

return requestCached;

}


public void setRequestCached(Boolean requestCached) {

this.requestCached = requestCached;

}


public Integer getRequestCacheExpiry() {

return requestCacheExpiry;

}


public void setRequestCacheExpiry(Integer requestCacheExpiry) {

this.requestCacheExpiry = requestCacheExpiry;

}


public List<Monday> getMonday() {

return monday;

}


public void setMonday(List<Monday> monday) {

this.monday = monday;

}


}

该Genre班


public class Genre {


@SerializedName("mal_id")

@Expose

private Integer malId;

@SerializedName("type")

@Expose

private String type;

@SerializedName("name")

@Expose

private String name;

@SerializedName("url")

@Expose

private String url;


public Integer getMalId() {

return malId;

}


public void setMalId(Integer malId) {

this.malId = malId;

}


public String getType() {

return type;

}


public void setType(String type) {

this.type = type;

}


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;

}


}

最后是Monday班级


public class Monday {


@SerializedName("mal_id")

@Expose

private Integer malId;

@SerializedName("url")

@Expose

private String url;

@SerializedName("title")

@Expose

private String title;

@SerializedName("image_url")

@Expose

private String imageUrl;

@SerializedName("synopsis")

@Expose

private String synopsis;

@SerializedName("type")

@Expose

private String type;

@SerializedName("airing_start")

@Expose

private String airingStart;

@SerializedName("episodes")

@Expose

private Integer episodes;

@SerializedName("members")

@Expose

private Integer members;

@SerializedName("genres")

@Expose

private List<Genre> genres = null;

@SerializedName("source")

@Expose

private String source;

@SerializedName("producers")

@Expose

private List<Object> producers = null;

@SerializedName("score")

@Expose

private Object score;

@SerializedName("licensors")

@Expose

private List<Object> licensors = null;

@SerializedName("r18")

@Expose

private Boolean r18;

@SerializedName("kids")

@Expose

private Boolean kids;


public Integer getMalId() {

return malId;

}


public void setMalId(Integer malId) {

this.malId = malId;

}


public String getUrl() {

return url;

}


public void setUrl(String url) {

this.url = url;

}


public String getTitle() {

return title;

}


public void setTitle(String title) {

this.title = title;

}


public String getImageUrl() {

return imageUrl;

}


public void setImageUrl(String imageUrl) {

this.imageUrl = imageUrl;

}


public String getSynopsis() {

return synopsis;

}


public void setSynopsis(String synopsis) {

this.synopsis = synopsis;

}


public String getType() {

return type;

}


public void setType(String type) {

this.type = type;

}


public String getAiringStart() {

return airingStart;

}


public void setAiringStart(String airingStart) {

this.airingStart = airingStart;

}


public Integer getEpisodes() {

return episodes;

}


public void setEpisodes(Integer episodes) {

this.episodes = episodes;

}


public Integer getMembers() {

return members;

}


public void setMembers(Integer members) {

this.members = members;

}


public List<Genre> getGenres() {

return genres;

}


public void setGenres(List<Genre> genres) {

this.genres = genres;

}


public String getSource() {

return source;

}


public void setSource(String source) {

this.source = source;

}


public List<Object> getProducers() {

return producers;

}


public void setProducers(List<Object> producers) {

this.producers = producers;

}


public Object getScore() {

return score;

}


public void setScore(Object score) {

this.score = score;

}


public List<Object> getLicensors() {

return licensors;

}


public void setLicensors(List<Object> licensors) {

this.licensors = licensors;

}


public Boolean getR18() {

return r18;

}


public void setR18(Boolean r18) {

this.r18 = r18;

}


public Boolean getKids() {

return kids;

}


public void setKids(Boolean kids) {

this.kids = kids;

}


}



查看完整回答
反对 回复 2021-11-17
?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

您的 api 链接是https://api.jikan.moe/v3/schedule 但在您编写的代码中


public static final String BASE_URL = "http://api.themoviedb.org/3/";

您可以将基本 url 更改为https://api.jikan.moe/v3/,或者

在 getSchedule() 的 GET 注释上提供完整的 url


 @GET("https://api.jikan.moe/v3/schedule")

 Call<MainResponse> getSchedule();


查看完整回答
反对 回复 2021-11-17
  • 2 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信