[英]error in get response from api using retrofit to fetch it in recycleview
I'm trying to network with this api: https://api.jikan.moe/v3/schedule 我正在尝试与此API联网: https : //api.jikan.moe/v3/schedule
so I use retrofit by create ApiClient.java class and this is the code of it: 所以我通过创建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;
}
} }
and the interface for complete endpoint of link is like this: 链接的完整端点的接口是这样的:
public interface ApiInterface {
@GET("schedule")
Call<MainResponse> getSchedule();
}
so I used serializable in modeling data and I create the MainResponse.java class to get the main monday array in api: 因此,我在数据建模中使用了可序列化的代码,并创建了MainResponse.java类来获取api中的主要星期一数组:
public class MainResponse { 公共类MainResponse {
@SerializedName("monday")
private List<Schedule> monday;
public List<Schedule> getMonday() {
return monday;
}
public void setMonday(List<Schedule> monday) {
this.monday = monday;
}
} }
then I create a new modeling class to get the list of object items of monday array in class Schedule.java: 然后创建一个新的建模类,以在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;
}
}
finally in MainActivity I call this: 最后在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());
}
});
as you can see I used an recycleview to fetch the titles of monday array but the problem is when I run the app it's just crushed because of this: 如您所见,我使用了recycleview来获取星期一数组的标题,但是问题是当我运行应用程序时,它由于以下原因而被压碎了:
Attempt to invoke virtual method 'java.util.List com.example.user_pc.capstonestage2.MainResponse.getMonday()' on a null object reference 尝试在空对象引用上调用虚拟方法'java.util.List com.example.user_pc.capstonestage2.MainResponse.getMonday()'
and this is the whole dialog of error: 这是整个错误对话框:
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)
I can't understand why it's null 我不明白为什么它为空
I found this error too in the logcat: 我也在logcat中发现了此错误:
10-14 21:02:12.373 9046-9046/com.example.user_pc.capstonestage2 E/RecyclerView: No adapter attached; 10-14 21:02:12.373 9046-9046 / com.example.user_pc.capstonestage2 E / RecyclerView:未连接适配器; skipping layout 跳过布局
so if you need the code of adapter this is it: 因此,如果您需要适配器的代码,就是这样:
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();
}
}
You have declared wrong the API URL is : 您已声明错误的API URL为:
public static final String BASE_URL = "https://api.jikan.moe/v3/";
Your MainResponse
should be like this : 您的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;
}
}
The Genre
class 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;
}
}
And finally the Monday
class 最后是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;
}
}
Your api link is https://api.jikan.moe/v3/schedule but in your code you wrote 您的api链接为https://api.jikan.moe/v3/schedule,但是在您编写的代码中
public static final String BASE_URL = "http://api.themoviedb.org/3/";
You can either change the base url to https://api.jikan.moe/v3/
, or 您可以将基本URL更改为https://api.jikan.moe/v3/
,或者
provide the full url on GET annotation of getSchedule() 提供有关getSchedule()的GET注释的完整URL
@GET("https://api.jikan.moe/v3/schedule")
Call<MainResponse> getSchedule();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.