我在服务器中放置了一个JSON文件,该文件描述了一个学期的课程安排。我必须使用Volley请求它。稍后我需要单独序列化返回的JSON对象,并为该特定月份创建多个事件。
但是当我运行应用程序时,Volley返回onErrorResponse。当我尝试使用Retrofit做同样的事情时,它工作正常,但输出并不像预期的那样。
IT-3-2.json
[{"schedule_of":"IT-3-2","day":"1","from_time":"09:55","to_time":"10:45","subject":"CC","details":"manasa"},
{"schedule_of":"IT-3-2","day":"1","from_time":"10:45","to_time":"11:35","subject":"CC","details":"manasa"},
{"schedule_of":"IT-3-2","day":"1","from_time":"11:35","to_time":"12:25","subject":"WT","details":"asha"},
{"schedule_of":"IT-3-2","day":"1","from_time":"12:25","to_time":"13:15","subject":"WT","details":"asha"},
{"schedule_of":"IT-3-2","day":"1","from_time":"14:15","to_time":"15:05","subject":"ACS lab","details":"gouthami"},
{"schedule_of":"IT-3-2","day":"1","from_time":"15:05","to_time":"15:55","subject":"ACS lab","details":"gouthami"},
{"schedule_of":"IT-3-2","day":"1","from_time":"15:55","to_time":"16:45","subject":"ACS lab","details":"gouthami"},
{"schedule_of":"IT-3-2","day":"2","from_time":"09:55","to_time":"10:45","subject":"IPR","details":"samreen"},
{"schedule_of":"IT-3-2","day":"2","from_time":"10:45","to_time":"11:35","subject":"IPR","details":"samreen"},
{"schedule_of":"IT-3-2","day":"2","from_time":"11:35","to_time":"12:25","subject":"WT","details":"asha"},
{"schedule_of":"IT-3-2","day":"2","from_time":"12:25","to_time":"13:15","subject":"WT","details":"asha"},
{"schedule_of":"IT-3-2","day":"2","from_time":"14:15","to_time":"15:05","subject":"WT lab","details":"rajesh"},
{"schedule_of":"IT-3-2","day":"2","from_time":"15:05","to_time":"15:55","subject":"WT lab","details":"rajesh"},
{"schedule_of":"IT-3-2","day":"2","from_time":"15:55","to_time":"16:45","subject":"WT lab","details":"rajesh"},
{"schedule_of":"IT-3-2","day":"3","from_time":"09:55","to_time":"10:45","subject":"DWDM","details":"shashikanth"},
{"schedule_of":"IT-3-2","day":"3","from_time":"10:45","to_time":"11:35","subject":"DWDM","details":"shashikanth"},{"schedule_of":"IT-3-2","day":"3","from_time":"11:35","to_time":"12:25","subject":"OOAD","details":"madhukar"},{"schedule_of":"IT-3-2","day":"3","from_time":"12:25","to_time":"13:15","subject":"OOAD","details":"madhukar"},{"schedule_of":"IT-3-2","day":"4","from_time":"09:55","to_time":"10:45","subject":"STM","details":"harikrishna"},{"schedule_of":"IT-3-2","day":"4","from_time":"10:45","to_time":"11:35","subject":"STM","details":"harikrishna"},{"schedule_of":"IT-3-2","day":"4","from_time":"11:35","to_time":"12:25","subject":"OOAD","details":"shashikanth"},{"schedule_of":"IT-3-2","day":"4","from_time":"12:25","to_time":"13:15","subject":"OOAD","details":"shashikanth"},{"schedule_of":"IT-3-2","day":"4","from_time":"14:15","to_time":"15:05","subject":"DWDM","details":"shashikanth"},{"schedule_of":"IT-3-2","day":"4","from_time":"15:05","to_time":"15:55","subject":"DWDM","details":"shashikanth"},{"schedule_of":"IT-3-2","day":"5","from_time":"09:55","to_time":"10:45","subject":"IPR","details":"samreen"},{"schedule_of":"IT-3-2","day":"5","from_time":"10:45","to_time":"11:35","subject":"IPR","details":"samreen"},{"schedule_of":"IT-3-2","day":"5","from_time":"11:35","to_time":"12:25","subject":"STM","details":"harikrishna"},{"schedule_of":"IT-3-2","day":"5","from_time":"12:25","to_time":"13:15","subject":"STM","details":"harikrishna"},{"schedule_of":"IT-3-2","day":"5","from_time":"14:15","to_time":"15:05","subject":"CC","details":"manasa"},{"schedule_of":"IT-3-2","day":"5","from_time":"15:05","to_time":"15:55","subject":"CC","details":"manasa"}]
AsynchronousActivity.java
@Override
public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
visibleMonth = newMonth;
visibleYear = newYear;
Log.d(TAG, "month changed - "+newMonth);
final String URL = "http://192.168.0.5/practice/schedule/jsons/IT-3-2.json";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "AbCdEfGh123456");
JsonObjectRequest req = new JsonObjectRequest(URL,null /*new JSONObject(params)*/,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Gson gson = new Gson();
VolleyLog.v("Response:%n %s", response);
makeWeeklyEvents((List<WeeklyEvent>) gson.fromJson(response.toString(),WeeklyEvent.class) );
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
// add the request object to the queue to be executed
//AppController.getInstance().addToRequestQueue(req);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(req);
// Return only the events that matches newYear and newMonth.
List<WeekViewEvent> matchedEvents = new ArrayList<WeekViewEvent>();
for (WeekViewEvent event : events) {
if (eventMatches(event, newYear, newMonth)) {
matchedEvents.add(event);
}
}
return matchedEvents;
}
public void makeWeeklyEvents(List<WeeklyEvent> events){
this.events.clear();
for (WeeklyEvent event : events) {
this.events.addAll(event.toWeekViewEvent());
Log.d(TAG, "size of events : "+this.events.size());
}
getWeekView().notifyDatasetChanged();
}
weeklyEvent.java
public class WeeklyEvent {
private static final String TAG = WeeklyEvent.class.getSimpleName();
@Expose
@SerializedName("subject")
private String mName;
@Expose @SerializedName("day")
private int mDay;
@Expose @SerializedName("from_time")
private String mStartTime;
@Expose @SerializedName("to_time")
private String mEndTime;
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public int getDay() {
return mDay;
}
public void setDay(int day) {
this.mDay = day;
}
public String getStartTime() {
return mStartTime;
}
public void setStartTime(String startTime) {
this.mStartTime = startTime;
}
public String getEndTime() {
return mEndTime;
}
public void setEndTime(String endTime) {
this.mEndTime = endTime;
}
@SuppressLint("SimpleDateFormat")
public ArrayList<WeekViewEvent> toWeekViewEvent(){
// Parse time.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date start = new Date();
Date end = new Date();
try {
start = sdf.parse(getStartTime());
} catch (ParseException e) {
e.printStackTrace();
}
try {
end = sdf.parse(getEndTime());
} catch (ParseException e) {
e.printStackTrace();
}
// Initialize start and end time.
Calendar now = Calendar.getInstance();
// Create an week view event.
ArrayList<WeekViewEvent> list = new ArrayList<WeekViewEvent>();
// Create a WeekViewEvent holder
WeekViewEvent weekViewEvent = new WeekViewEvent();
for(int i= getFirstDay(1,AsynchronousActivity.visibleMonth,AsynchronousActivity.visibleYear) - getDay(); i<now.getActualMaximum(Calendar.DAY_OF_MONTH);i+=7 ){
Calendar startTime = (Calendar) now.clone();
startTime.setTimeInMillis(start.getTime());
startTime.set(Calendar.YEAR, AsynchronousActivity.visibleYear);
startTime.set(Calendar.MONTH, AsynchronousActivity.visibleMonth);
startTime.set(Calendar.DAY_OF_MONTH, i);
Calendar endTime = (Calendar) startTime.clone();
endTime.setTimeInMillis(end.getTime());
endTime.set(Calendar.YEAR, AsynchronousActivity.visibleYear);
endTime.set(Calendar.MONTH, AsynchronousActivity.visibleMonth);
endTime.set(Calendar.DAY_OF_MONTH, i);
// Set values into event.
weekViewEvent.setName(getName());
weekViewEvent.setStartTime(startTime);
weekViewEvent.setEndTime(endTime);
weekViewEvent.setColor(Color.GREEN);
Log.d(TAG, "weekly event recieved : "+getName()+", time : "+weekViewEvent.getStartTime().getTimeInMillis() +" - "+weekViewEvent.getEndTime().getTimeInMillis());
// Append WeekViewEvent to the list.
list.add(weekViewEvent);
}
Log.d(TAG, "list is updating "+list.size()+" events.");
return list;
}
public int getFirstDay(int day, int month, int year)
{
Calendar cal = new GregorianCalendar();
cal.set(Calendar.DATE, day);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.MONDAY:
return 0;
case Calendar.TUESDAY:
return 1;
case Calendar.WEDNESDAY:
return 2;
case Calendar.THURSDAY:
return 3;
case Calendar.FRIDAY:
return 4;
case Calendar.SATURDAY:
return 5;
case Calendar.SUNDAY:
return 6;
}
return -1;
}
}
运行该应用程序,它会返回以下排球错误:screenshot of logcat
当我从设备浏览器尝试相同的URL链接时,可以访问JSON文件。
当我尝试使用Retrofit做同样的事情时,它成功检索并处理了JSON,但输出并不像预期的那样。
if (!calledNetwork) {
RestAdapter retrofit = new RestAdapter.Builder()
.setEndpoint("http://192.168.0.5")
.build();
MyJsonService service = retrofit.create(MyJsonService.class);
service.listWeekEvents(this);
calledNetwork = true;
MyJsonService.java
public interface MyJsonService {
@GET("/practice/schedule/jsons/IT-3-2.json")
//http://192.168.0.5/practice/schedule/jsons/IT-3-2.json
void listWeekEvents(Callback<List<WeeklyEvent>> weeklyEventsCallback);
}
功能是,我需要复制输入事件-JsonObject(它包含“day”-day_of_week,表示该事件每周发生在那一天)到一个月内每周的所有特定日期。
for(int i= getFirstDay(1,AsynchronousActivity.visibleMonth,AsynchronousActivity.visibleYear) - getDay(); i<now.getActualMaximum(Calendar.DAY_OF_MONTH);i+=7 ){
Calendar startTime = (Calendar) now.clone();
startTime.setTimeInMillis(start.getTime());
startTime.set(Calendar.YEAR, AsynchronousActivity.visibleYear);
startTime.set(Calendar.MONTH, AsynchronousActivity.visibleMonth);
startTime.set(Calendar.DAY_OF_MONTH, i);
Calendar endTime = (Calendar) startTime.clone();
endTime.setTimeInMillis(end.getTime());
endTime.set(Calendar.YEAR, AsynchronousActivity.visibleYear);
endTime.set(Calendar.MONTH, AsynchronousActivity.visibleMonth);
endTime.set(Calendar.DAY_OF_MONTH, i);
// Set values into event.
weekViewEvent.setName(getName());
weekViewEvent.setStartTime(startTime);
weekViewEvent.setEndTime(endTime);
weekViewEvent.setColor(Color.GREEN);
Log.d(TAG, "weekly event recieved : "+getName()+", time : "+weekViewEvent.getStartTime().getTimeInMillis() +" - "+weekViewEvent.getEndTime().getTimeInMillis());
// Append WeekViewEvent to the list.
list.add(weekViewEvent);
}
申请时(2017年2月14日),活动仅在2017年2月24日至2017年2月28日期间展示。生成的所有重复事件都会在一天内显示出来。
screenshot of the application showing 26th Feb,2017 while runned with retrofit
我一直在使用alamkanak / Android-Week-View(如果需要,链接将发布在答案部分,因为目前无法发布超过2个链接)库来显示事件。
请帮我解决这个问题。提前谢谢。