Java 使用改型在recycleview中获取api响应时出错
我正在尝试使用以下api建立网络: 因此,我通过创建ApiClient.java类来使用改型,代码如下:Java 使用改型在recycleview中获取api响应时出错,java,android,Java,Android,我正在尝试使用以下api建立网络: 因此,我通过创建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==nul
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();
}
最后,在main活动中,我称之为:
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>() {
@Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
int statusCode = response.code();
List<Schedule> schedule = response.body().getMonday();
recyclerView.setAdapter(new MainAdapter(schedule, R.layout.list_item_schedule, getApplicationContext()));
}
@Override
public void onFailure(Call<MainResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
我不明白为什么它是空的
我在logcat中也发现了这个错误:
10-14 21:02:12.373 9046-9046/com.example.user_pc.capstonestage 2
E/RecyclerView:未连接适配器;跳过布局
因此,如果您需要适配器的代码,这就是:
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MovieViewHolder> {
private List<Schedule> schedule;
private int rowLayout;
private Context context;
public static class MovieViewHolder extends RecyclerView.ViewHolder {
LinearLayout schedulesLayout;
TextView title;
public MovieViewHolder(View v) {
super(v);
schedulesLayout = (LinearLayout) v.findViewById(R.id.schedule_layout);
title = (TextView) v.findViewById(R.id.title);
}
}
public MainAdapter(List<Schedule> schedule, int rowLayout, Context context) {
this.schedule = schedule;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public MainAdapter.MovieViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new MovieViewHolder(view);
}
@Override
public void onBindViewHolder(MovieViewHolder holder, final int position) {
holder.title.setText(schedule.get(position).getTitle());
}
@Override
public int getItemCount() {
return schedule.size();
}
}
公共类MainAdapter扩展了RecyclerView.Adapter{
私人名单附表;
私人室内布局;
私人语境;
公共静态类MovieViewHolder扩展了RecyclerView.ViewHolder{
线性布局计划;
文本视图标题;
公众电影观众(视图v){
超级(五);
schedulesLayout=(线性布局)v.findViewById(R.id.schedule_布局);
title=(TextView)v.findViewById(R.id.title);
}
}
公共MainAdapter(列表计划、int行布局、上下文){
这个。时间表=时间表;
this.rowLayout=rowLayout;
this.context=上下文;
}
@凌驾
public MainAdapter.MovieViewHolder onCreateViewHolder(视图组父级、,
int视图类型){
View=LayoutInflater.from(parent.getContext()).flate(rowLayout,parent,false);
返回新的MoviewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(MovieViewHolder,最终int位置){
holder.title.setText(schedule.get(position.getTitle());
}
@凌驾
public int getItemCount(){
返回时间表。大小();
}
}
您声明的API URL是错误的:
public static final String BASE_URL = "https://api.jikan.moe/v3/";
您的main响应应如下所示:
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;
}
}
最后是周一
课
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;
}
}
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;
}
}
周一公开课{
@SerializedName(“mal_id”)
@暴露
私有整数malId;
@序列化名称(“url”)
@暴露
私有字符串url;
@序列化名称(“标题”)
@暴露
私有字符串标题;
@SerializedName(“图像url”)
@暴露
私有字符串imageUrl;
@序列化名称(“概要”)
@暴露
私有字符串概要;
@序列化名称(“类型”)
@暴露
私有字符串类型;
@序列化名称(“播放开始”)
@暴露
私有字符串airingStart;
@序列化名称(“剧集”)
@暴露
私人整数集;
@序列化名称(“成员”)
@暴露
私有整数成员;
@序列化名称(“流派”)
@暴露
私有列表类型=空;
@SerializedName(“源”)
@暴露
私有字符串源;
@序列化名称(“生产者”)
@暴露
私有列表生产者=null;
@序列化名称(“分数”)
@暴露
私人物品评分;
@序列化名称(“许可方”)
@暴露
私有列表许可方=空;
@序列化名称(“r18”)
@暴露
私有布尔r18;
@序列化名称(“儿童”)
@暴露
私生子;
公共整数getMalId(){
返回malId;
}
public void setMalId(整数malId){
this.malId=malId;
}
公共字符串getUrl(){
返回url;
}
公共void setUrl(字符串url){
this.url=url;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getImageUrl(){
返回imageUrl;
}
public void setImageUrl(字符串imageUrl){
this.imageUrl=imageUrl;
}
公共字符串getSynosis(){
返回概要;
}
公共无效设置同步(字符串概要){
this.synosis=大纲;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共字符串getAiringStart(){
回风启动;
}
公共void设置airingStart(字符串airingStart){
this.airingStart=airingStart;
}
公共整数集(){
回归集;
}
公共电视剧集(整集){
这个。插曲=插曲;
}
公共整数getMembers(){
返回成员;
}
公共void集合成员(整数成员){
这个.成员=成员;
}
公共列表getGenres(){
回归体裁;
}
公共类型(列表类型){
this.genres=流派;
}
公共字符串getSource(){
返回源;
}
公共void集合源(字符串源){
this.source=源;
}
公共列表getProducers(){
回报生产者;
}
公共生产商(列出生产商){
这个。生产者=生产者;
}
公共对象getScore(){
返回分数;
}
公共核心(目标分数){
这个分数=分数;
}
公共列表GetLicensor(){
返还许可人;
}
公共无效设置许可人(列出许可人){
this.licensors=许可方;
}
公共布尔getR18(){
返回r18;
}
公共无效集合r18(布尔值r18){
这是r18=r18;
}
公共布尔getKids(){
归还孩子;
}
公共无效集合子对象(布尔子对象){
这个。孩子=孩子;
}
}
您的api链接是
但是在你的代码里你写的
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")
调用getSchedule();
你把基本URL搞错了,你把“v”留在了“v3”中,你必须把它改成哦,天哪,我花了两天时间处理这个愚蠢的错误!非常感谢,但是我可以问你一个问题吗?如果我想在其他日子,比如周二等,在同一个活动中,在一个回收视图中,我怎么做@斯基佐奥兹ᴉʞ要做到这一点,我需要更多信息,即您的json
文件在哪里显示日期以及您希望如何将信息获取到recyclerview:Dokay非常感谢我将创建一个新问题并向您发送链接@Skizo ozᴉʞS
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;
}
}
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;
}
}
public static final String BASE_URL = "http://api.themoviedb.org/3/";
@GET("https://api.jikan.moe/v3/schedule")
Call<MainResponse> getSchedule();