Using ExchangeService I need to get Calendar folder and retrieve all events there
It's done by this piece of code
private List getRoomCalendar() throws Exception {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials(username, password);
service.setCredentials(credentials);
service.setUrl(new URI(msExchangeUrl));
FolderView fv = new FolderView(100);
fv.setTraversal(FolderTraversal.Deep);
FolderId confRoomFolderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox(username));
System.out.println(confRoomFolderId.getFolderName());
List events = new ArrayList();
Date date = new Date();
try {
CalendarFolder calendarFolder = CalendarFolder.bind(service, confRoomFolderId);
CalendarView cView = new CalendarView(new DateTime(date).minusDays(1).toDate(),
new DateTime(date).plusDays(1).toDate(), 100);
cView.setPropertySet(new PropertySet(AppointmentSchema.Subject,
AppointmentSchema.Start,
AppointmentSchema.End));
// we can set other properties as well depending upon our need.
FindItemsResults appointments = calendarFolder.findAppointments(cView);
List<Appointment> appList = appointments.getItems();
for (Appointment appointment : appList) {
Map event = readEvent(appointment);
events.add(event);
}
} catch (Exception e) {
e.printStackTrace();
}
return events;
}
private Map readEvent(Appointment appointment) {
Map appointmentData = new HashMap();
try {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
appointmentData.put("appointmentSubject", appointment.getSubject());
appointmentData.put("appointmentStartTime", df.format(appointment.getStart()));
appointmentData.put("appointmentEndTime", df.format(appointment.getEnd()));
} catch (ServiceLocalException e) {
e.printStackTrace();
}
return appointmentData;
}
Cool, it's working and returns these data
default MS Calendar
The question is: how can I get events for MY CUSTOM CALENDAR called name
see here
custom MS calendar
The solution is to create the folderView first. Afterwards, apply a search filter, find it using FindFolderResults.
PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
FolderView fvFolderView = new FolderView(1);
fvFolderView.setPropertySet(psPropset);
SearchFilter SfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName,"name");
FindFoldersResults findFoldersResults = service.findFolders(confRoomFolderId, SfSearchFilter, fvFolderView);
System.out.println(findFoldersResults.getFolders().get(0).getDisplayName());
My Get and Set return null.
I think it's missing something on my Set method.
My Authen.java :
public class Authen {
String sessionID;
public void setSessionID(String sessionID) {
this.sessionID = sessionID;
}
public String getSessionID(){
return this.sessionID;
}
}
My Set method :
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
My Get method returns null when I log sID
Authen at = new Authen();
String sID = at.getSessionID();
You're redeclaring at which is going to remove any values you set.
After you set the id, don't create a new Authen object
So change this:
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
Authen at = new Authen(); // This is where you create a new EMPTY authen
String sID = at.getSessionID();
to:
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
String sID = at.getSessionID();
You should use at like below...
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
String sID = at.getSessionID();
No need to renew at.
The reason why your sID is null is that you 'new' a brand new object as you declare 'at' object in the second time Authen at = new Authen();.
You do not need to initianite Authen again if you want to get the sID correctly.
Just do:
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
String sID = at.getSessionID();
You can transfer the Authen instance from class A to B if it is needed there (assuming class A creates the instance of B):
class A {
private void aMethod() {
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
...
B b = new B(at);
}
}
class B {
private Authen authen;
public B(Authen at) {
this.authen = at;
...
}
private void anotherMethod() {
String sID = this.authen.getSessionID();
...
}
}
obviously this code is not complete, just to show the main idea - and just one of many possibilities.
I have a JSON file placed in the server which describes one academic week schedule of a class. I have to request for it using Volley. Later I need to serialize the returned JSON object individually and create multiple events for that particular month.
But when I run the application, Volley returns onErrorResponse. When I try doing the same with Retrofit, it worked fine but the output is not as expected.
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;
}
}
Running the application, it returns the follow Volley error: screenshot of logcat
JSON file is accessible when I try the same URL link from the device browser.
When I tried doing the same with Retrofit, it successfully retrieved and processed the JSON but the output was not as expected.
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);
}
The functionality is that, I need to duplicate the input event- JsonObject (it contains "day"-day_of_week which represents that event occurs every week on that day) to all that particular day of every week in one month.
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);
}
When the application is runned (on 14th Feb,2017), the events are shown only from 24th Feb,2017 - 28th Feb,2017. and that to all the duplicate events generated are shown in one day itself.
screenshot of the application showing 26th Feb,2017 while runned with retrofit
I have been using alamkanak/Android-Week-View (link will be posted in answers section if wanted, as more then 2 links can't be posted for now) library to display the events.
Please, help me to solve this problem. Thanks in advance.