Quantcast
Channel: Banckle Collaborator Blog» Videos & Tutorials
Viewing all articles
Browse latest Browse all 2

Listing Upcoming Meetings using Banckle API – an example with Java

$
0
0

This tutorial demonstrates one of the use-cases of Banckle Online Meeting APIs. It performs two major tasks; first is to authenticate with Banckle APIs and the second is to retrieve a list of upcoming meetings.

Prerequisites

  • Basic understanding of Java programming language.
  • Basic understanding of JavaServer Pages (JSP) technology.
  • You have installed Apache Maven.

Create project with Maven

Maven is a great tool to start with. Lets create an empty JSP project using Maven. Please run the following command to get started:

  1.  
  2. mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes \
  3. -DarchetypeArtifactId=webapp-javaee6 \
  4. -DarchetypeVersion=1.5 \
  5. -DarchetypeRepository=http://repo1.maven.org/maven2 \
  6. -DgroupId=com.banckle.marketplace.examples \
  7. -DartifactId=meetinglist1 \
  8. -Dversion=1.0-SNAPSHOT \
  9. -Dpackage=com.banckle.marketplace.examples.meetinglist1 \
  10. -Darchetype.interactive=false --batch-mode
  11.  

Add External Libraries

Maven uses pom.xml file in main project directory for configuration and dependencies. Add JSON to dependencies section of pom.xml file.

  1.  
  2. <dependencies>
  3. ...
  4. <dependency>
  5. <groupId>org.json</groupId>
  6. <artifactId>json</artifactId>
  7. <version>20090211</version>
  8. </dependency>
  9. ...
  10. </dependencies>
  11.  

Let's Code

You are ready to start coding. Our code will reside inside src/main/webapp/index.jsp. Use your favorite text editor to edit it.

First we import some external classes by adding the following lines near the top of index.jsp.

  1.  
  2. <%@page import="java.io.*"%>
  3. <%@page import="java.util.*"%>
  4. <%@page import="java.net.*"%>
  5. <%@page import="org.json.*"%>
  6.  

Define a convenience method for Banckle API calls. Let our api method be:

  1.  
  2. public JSONObject api(String url) throws ServletException {
  3. ...
  4. }
  5.  

Open a network connection to call our API.

  1.  
  2. URLConnection connection = new URL(url).openConnection();
  3.  

Retrieve the API response and save to a string.

  1.  
  2. String resp = new Scanner(connection.getInputStream()).useDelimiter("\\A").next();
  3.  

Convert the response string to JSONObject. Read Javadoc of JSONObject for more information.

  1.  
  2. JSONObject json = new JSONObject(resp);
  3.  

Check for possible error messages returned by the API call.

  1.  
  2. if (json.has("error")) {
  3. throw new ServletException("API error: " + json.toString());
  4. }
  5.  

And return the result if everything was fine.

  1.  
  2. return json;
  3.  

Putting things together, we come up with the following method. You can either add this method to the JSP page, or create a separate class for that. For simplicity, I have put everything in a try-catch block. You can handle each exception separately.

  1.  
  2. public JSONObject api(String url) throws ServletException {
  3. try {
  4. URLConnection connection = new URL(url).openConnection();
  5. String resp = new Scanner(connection.getInputStream()).useDelimiter("\\A").next();
  6. JSONObject json = new JSONObject(resp);
  7. if (json.has("error")) {
  8. throw new ServletException("Check log for errors: " + json.toString());
  9. }
  10. return json;
  11. } catch (Exception x) {
  12. throw new ServletException(x);
  13. }
  14. }
  15.  

Banckle APIs are protected by authentication token, which can be retrieved by calling the authentication API. You can signup for a Banckle account for free. The same token is valid and reusable for subsequent API calls. It is highly discouraged to retrieve a new token for each call.

  1.  
  2. JSONObject auth = api("https://apps.banckle.com/api/authenticate?product=meeting&userid=USERNAME&password=PASSWORD");
  3. String token = auth.getJSONObject("return").getString("token");
  4.  

Now using the above authentication token, we call Get Company's Upcoming Meeting List, Banckle Online Meeting API.

  1.  
  2. JSONObject meetings = api("https://apps.banckle.com/meeting/api/getCompanyUpcomingMeetings?token=" + token);
  3.  

You are ready to display the meeting list. Use a loop to display the entries in meetings object.

  1.  
  2. for (int i = 0; i < meetings.getJSONArray("return").length(); i++) {
  3. String subject = meetings.getJSONArray("return").getJSONObject(i).getString("subject");
  4. String id = meetings.getJSONArray("return").getJSONObject(i).getString("id");
  5. String meetingurl = "https://apps.banckle.com/meeting/meeting?id=" + id;
  6. ...
  7. }
  8.  

Again, putting things all together we have a complete working application. I have attached a working project. You can download and extract it.

Compile the Code

Use Maven to compile and package the project for us. From the project directory run the following command.

  1.  
  2. mvn package
  3.  

Your project is ready in target/meetinglist1-1.0-SNAPSHOT.war file. You can deploy it in your favorite Servlet server like Tomcat. I have tested it on Glassfish.

Our official feedback community is always open for your valued suggestions. In case you require more information or want to share any suggestion or idea please feel free to post there.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images