Skip to content

Instantly share code, notes, and snippets.

@yun14u
Forked from chaitut715/SakaiUtil.java
Created December 28, 2016 17:32
Show Gist options
  • Save yun14u/69f0a911a1d3342f9f23b0930decef5b to your computer and use it in GitHub Desktop.
Save yun14u/69f0a911a1d3342f9f23b0930decef5b to your computer and use it in GitHub Desktop.
get calendar events of a user in sakai
package com.chaitu.lmscalendar.sakai.util;
import android.util.Log;
import com.chaitu.lmscalendar.settings.Settings;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.CookieStore;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.client.protocol.HttpClientContext;
import cz.msebera.android.httpclient.impl.client.BasicCookieStore;
import cz.msebera.android.httpclient.impl.client.HttpClientBuilder;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.protocol.BasicHttpContext;
import cz.msebera.android.httpclient.protocol.HttpContext;
import cz.msebera.android.httpclient.util.EntityUtils;
/**
* Created by Chaitanya on 11/14/2016.
*/
public class SakaiUtil{
public static final String TAG = "SakaiUtil";
ObjectMapper mapper = new ObjectMapper();
public static String getEventsResponse(final String url, final String username, final String password) {
try {
Log.i(TAG, "doInBackground:1");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("_username", "chaitanya"));
params.add(new BasicNameValuePair("_password", "pass"));
//create httpclient
HttpClient httpClient = HttpClientBuilder.create().build();
Log.i(TAG, "doInBackground:2");
//store cookies in context
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
Log.i(TAG, "doInBackground:3");
//call login web service
String webserviceURL = url+"/direct/session/new";
HttpPost postRequest = new HttpPost(webserviceURL);
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
postRequest.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(postRequest, httpContext);
Log.i(TAG, "doInBackground:4");
//get the status code
int status = response.getStatusLine().getStatusCode();
Log.i(TAG, "doInBackground:status:"+status);
//if status code is 201 login is successful. So reuse the httpContext for next requests.
//if status code is not 201, there is a problem with the request.
if(status!=201)
return null;
EntityUtils.consume( response.getEntity() );
Log.i(TAG, "doInBackground:5");
String responseVal = getCalendarData(httpContext, url);
//Log.i(TAG, "responseVal:"+responseVal);
//CalendarResponse calendarResponse = formatJSON(responseVal);
return responseVal;
}catch(IOException e){
e.printStackTrace();
return null;
}
}
protected static String getCalendarData(HttpContext httpContext, final String url) throws IOException {
Log.i(TAG, "getCalendarData:");
HttpClient httpClient = HttpClientBuilder.create().build();
String webserviceURL = url+"/direct/calendar/my.json";
HttpGet getRequest = new HttpGet(webserviceURL);
HttpResponse response = httpClient.execute(getRequest, httpContext);
HttpEntity httpEntity = response.getEntity();
String responseVal = EntityUtils.toString(httpEntity);
//Log.i(TAG, "responseVal:"+responseVal);
EntityUtils.consume(httpEntity);
return responseVal;
}
}
@yun14u
Copy link
Author

yun14u commented Dec 28, 2016

Initial fork

@yun14u
Copy link
Author

yun14u commented Dec 28, 2016

@chaitut715
Copy link

chaitut715 commented Dec 29, 2016

do you want sakai soap(axis or csf) call examples?
if yes, visit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment