gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:42:30 26 4
gpt4 key购买 nike

我正在尝试与此 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;
}

完整的链路端点接口(interface)是这样的:

public interface ApiInterface {

@GET("schedule")
Call<MainResponse> getSchedule();

}

所以我在建模数据中使用了可序列化,并创建了 MainResponse.java 类以在 api 中获取主要的星期一数组:

公共(public)类 MainResponse {

@SerializedName("monday")
private List<Schedule> monday;

public List<Schedule> getMonday() {
return monday;
}

public void setMonday(List<Schedule> monday) {
this.monday = monday;
}

然后我创建一个新的建模类来获取类 Schedule.java 中星期一数组的对象项列表:

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>() {
@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());
}
});

如您所见,我使用了回收 View 来获取星期一数组的标题,但问题是当我运行该应用程序时,它就因为这个而被压碎了:

Attempt to invoke virtual method 'java.util.List com.example.user_pc.capstonestage2.MainResponse.getMonday()' on a null object reference

这是整个错误对话框:

10-14 20:53:45.462 1050-1050/com.example.user_pc.capstonestage2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user_pc.capstonestage2, PID: 1050
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.user_pc.capstonestage2.MainResponse.getMonday()' on a null object reference
atcom.example.user_pc.capstonestage2.MainActivity$1.onResponse(MainActivity.java:36)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

我不明白为什么它是空的

我在 logcat 中也发现了这个错误:

10-14 21:02:12.373 9046-9046/com.example.user_pc.capstonestage2 E/RecyclerView: No adapter attached; skipping layout

所以如果你需要适配器的代码,就是这样:

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();
}
}

最佳答案

您声明错误的 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;
}

}

流派

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;
}

}

关于java - 使用改造从 api 获取响应以在 recycleview 中获取响应时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52805782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com