我正在使用 Retrofit 从 Ticketmaster API 获取数据 我的班级活动: `public class Event 实现 Parcelable { 私有字符串名称; 私有字符串 ID; 私有字符串 url; 私有字符串开始日期; 私有字符串时间; 私人字符串注释; 私有字符串图像; 私人布尔年龄限制; 私人 EventPlace 场所;
public Event() {}
public Event(String name, String id, String url, String image, String startDate,
String time, String notes, boolean ageRestrictions, EventPlace place) {
this.name = name;
this.id = id;
this.url = url;
this.startDate = startDate;
this.time = time;
this.notes = notes;
this.image = image;
this.ageRestrictions = ageRestrictions;
this.place = place;
}
protected Event(Parcel in) {
this.name = in.readString();
this.id = in.readString();
this.url = in.readString();
this.startDate = in.readString();
this.time = in.readString();
this.notes = in.readString();
this.image = in.readString();
this.ageRestrictions = in.readByte() != 0;
this.place = in.readParcelable(EventPlace.class.getClassLoader());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Boolean getAgeRestrictions() {
return ageRestrictions;
}
public void setAgeRestrictions(Boolean ageRestrictions) {
this.ageRestrictions = ageRestrictions;
}
public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
@Override
public Event createFromParcel(Parcel source) {
return new Event(source);
}
@Override
public Event[] newArray(int size) {
return new Event[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.id);
dest.writeString(this.url);
dest.writeString(this.startDate);
dest.writeString(this.time);
dest.writeString(this.notes);
dest.writeString(this.image);
dest.writeByte(this.ageRestrictions ? (byte) 1 : (byte) 0);
dest.writeParcelable(this.place, flags);
}
}
The main method is:
public void fetchEvents(String type, String city, String startDateTime, String time, long lastUpdate) {
long currentTime = System.currentTimeMillis();
// It gets the news from the Web Service if the last download
// of the news has been performed more than FRESH_TIMEOUT value ago
//TODO sistemare la condizione per l'aggiornamento
//if (currentTime - lastUpdate > FRESH_TIMEOUT) {
if (true) {
Call<EventApiResponse> eventsResponseCall = eventsApiService.getEvents(type, city, startDateTime, time,
"ymPPalpoNoG8lG5xyca0AQ6uhACG4y3j");
eventsResponseCall.enqueue(new Callback<EventApiResponse>() {
@Override
public void onResponse(@NonNull Call<EventApiResponse> call,
@NonNull Response<EventApiResponse> response) {
if(response.body() != null && response.isSuccessful()) {
List<Event> eventsList = response.body().getEvents();
Log.d(TAG, "Response code: " + response.code());
Log.d(TAG, "Response body: " + response.body());
responseCallback.onSuccess(eventsList, System.currentTimeMillis());
//TODO saveDataInDatabase(eventsList);
} else {
responseCallback.onFailure(application.getString(R.string.error_retrieving_events));
}
}
@Override
public void onFailure(@NonNull Call<EventApiResponse> call, @NonNull Throwable t) {
if(t.getMessage() != null)
Log.d(TAG, t.getMessage());
else
Log.d(TAG, "messaggio di errore nullo");
responseCallback.onFailure(t.getMessage());
}
});
} else {
//TODO`
调用方法为: ` public void onViewCreated(@NonNull View view, Bundle savingInstanceState) { super.onViewCreated(视图,savedInstanceState); recyclerViewEvents = view.findViewById(R.id.recyclerview); RecyclerView.LayoutManagerlayoutManager = new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false);
recyclerViewEvents.setLayoutManager(layoutManager);
recyclerViewEvents.setAdapter(recyclerViewEventsAdapter);
eventsRepository.fetchEvents("music", "324", "2024-06-01T08:00:00Z", "2024-06-30T08:00:00Z", 10);
}`
当我尝试从 json 本地文件解析时,它可以工作,但是当我尝试改造时,我得到一个 OK 响应代码(400),但主体为空。
您的请求有问题。响应代码 400 表示“错误请求”。你能分享 eventsApiService.getEvents 的样子吗?