猿问

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

我正在尝试使用此 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()”


缥缈止盈
浏览 147回答 2
2回答

FFIVE

您已声明错误的 API URL 是:public static final String BASE_URL = "https://api.jikan.moe/v3/";你MainResponse应该是这样的:public class MainResponse {@SerializedName("request_hash")@Exposeprivate String requestHash;@SerializedName("request_cached")@Exposeprivate Boolean requestCached;@SerializedName("request_cache_expiry")@Exposeprivate Integer requestCacheExpiry;@SerializedName("monday")@Exposeprivate 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")@Exposeprivate Integer malId;@SerializedName("type")@Exposeprivate String type;@SerializedName("name")@Exposeprivate String name;@SerializedName("url")@Exposeprivate 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")@Exposeprivate Integer malId;@SerializedName("url")@Exposeprivate String url;@SerializedName("title")@Exposeprivate String title;@SerializedName("image_url")@Exposeprivate String imageUrl;@SerializedName("synopsis")@Exposeprivate String synopsis;@SerializedName("type")@Exposeprivate String type;@SerializedName("airing_start")@Exposeprivate String airingStart;@SerializedName("episodes")@Exposeprivate Integer episodes;@SerializedName("members")@Exposeprivate Integer members;@SerializedName("genres")@Exposeprivate List<Genre> genres = null;@SerializedName("source")@Exposeprivate String source;@SerializedName("producers")@Exposeprivate List<Object> producers = null;@SerializedName("score")@Exposeprivate Object score;@SerializedName("licensors")@Exposeprivate List<Object> licensors = null;@SerializedName("r18")@Exposeprivate Boolean r18;@SerializedName("kids")@Exposeprivate 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;}}

qq_花开花谢_0

您的 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&nbsp;@GET("https://api.jikan.moe/v3/schedule")&nbsp;Call<MainResponse> getSchedule();
随时随地看视频慕课网APP

相关分类

Java
我要回答