Luis
Luis

Reputation: 35

Handling events in box-api with java

I´m newbie in box-api and i'm interesting in handling events with the new version of box java sdk. I've read the documentation about events and i have only found the following code.

I will be gratefull if anyone can help me with code, for example, handling a event of an upload of a file.

    BoxAPIConnection api = new BoxAPIConnection("YOUR-DEVELOPER-TOKEN");
    EventStream stream = new EventStream(api);
    stream.addListener(new EventListener() {
        public void onEvent(BoxEvent event) {
            // Handle the event.
            ???? Need help here ????
        }
    });
    stream.start();

Upvotes: 1

Views: 890

Answers (2)

Greg
Greg

Reputation: 3741

You're on the right track with your EventListener. In your onEvent(BoxEvent) method, you'll first want to filter on the type of events you're interested in with something like:

if (event.getType() == BoxEvent.Type.ITEM_UPLOAD) {
    // Do something
}

You can also find a full list of the supported event types in the javadocs.

Once you know the event type, you can cast the event source to the appropriate type. For example, if we're handling a BoxEvent.Type.ITEM_UPLOAD event, then the event source would be a BoxItem.

if (event.getType() == BoxEvent.Type.ITEM_UPLOAD) {
    BoxItem uploadedItem = (BoxItem) event.getSource();

    // Do something with the uploaded item. For this example, we'll just print
    // out its name.
    BoxItem.Info itemInfo = uploadedItem.getInfo();
    System.out.format("A file named '%s' was uploaded.\n", itemInfo.getName());
}

Upvotes: 2

Jure
Jure

Reputation: 799

Here you have list of events: https://developers.box.com/docs/

So where you have ??? in your code try

if(event == ITEM_UPLOAD) 
{
 //your action
}


or 

if(event == "ITEM_UPLOAD") 
{
{
 //your action
}
}

or this is probably correct:

if(event.type == "ITEM_UPLOAD") 
    {
     //your action
    }

And to see what event you are getting write this inside onEvent():

System.out.println("Event: " + event);

Upvotes: 0

Related Questions