Introduction
The Socialize SDK provides a simple set of classes and methods built upon the Socialize REST API
App developers can elect to use either the pre-defined user interface controls provided in the Socialize UI
framework, or “roll their own” using direct SDK calls.
ALL calls to the Socialize SDK are asynchronous, meaning that your application will not “block” while
waiting for a response from the Socialize server.
You are notified of the outcome of calls to the Socialize service via a SocializeListener
passed into each call to the Socialize SDK.
Initializing Socialize (Optional)
|
Advanced Use
|
The following section is not required if you are simply using the Action Bar
Note
Socialize will automatically initialize itself upon first use, however if you want more direct control over initialization you can initialize Socialize manually.
The Socialize SDK should be initialized in the onCreate() method of your Activity.
You should also include the onPause and onResume methods.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize socialize
Socialize.initAsync(this);
// Your code here
}
@Override
protected void onPause() {
super.onPause();
Socialize.onPause(this);
}
@Override
protected void onResume() {
super.onResume();
Socialize.onResume(this);
}
If you want to know about initialization success (e.g. if you want to access Socialize after init), you can also specify a listener
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize socialize
Socialize.initAsync(this, new SocializeInitListener() {
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onInit(Context context, IOCContainer container) {
// If you want to access Socialize directly, do it here
}
});
}
@Override
protected void onPause() {
super.onPause();
Socialize.onPause(this);
}
@Override
protected void onResume() {
super.onResume();
Socialize.onResume(this);
}
Entities
|
Advanced Use
|
The following section is not required if you are simply using the Action Bar
An entity is a single item of content in your app
Throughout the documentation and the code snippets we refer to an “entity”. This is simply a
generic term for something that can be view, shared, liked or commented on. Generally this will
correspond to a single item of content in your app.
Entities in Socialize MUST be associated with a unique key. It is recommended that where possible an
HTTP URL be used (i.e. one that corresponds to an active web page).
Using Entities
When posting a comment, share or like using Socialize an entity object must be provided.
This can either be an entity you created previously, or a new entity which will be created inline with the call to Socialize.
Note
You should always specify a name when creating an entity, however if you have previously created the entity with a name and do not want to specify the name every time you can provide a “null” argument for the name. This will instruct Socialize to ignore the name attribute and the name of the entity will NOT be updated.
Creating an Entity Inline
A typical use of an entity might be when posting a comment. Here the entity name is specified.
Entity entity = Entity.newInstance("key", "name");
// The "this" argument refers to the current Activity
CommentUtils.addComment(this, entity, "The comment", new CommentAddListener() {
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onCreate(Comment result) {
// Handle success
}
});
Alternatively a null argument can be specified for the name which will instruct the server to leave the existing name intact.
Entity entity = Entity.newInstance("key", null);
// The "this" argument refers to the current Activity
CommentUtils.addComment(this, entity, "The comment", new CommentAddListener() {
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onCreate(Comment result) {
// Handle success
}
});
Creating an Entity Explicitly
An entity consists of a key and a name. The name should be descriptive and help you identify the
entity when viewing reports on the Socialize dash board.
Creating an entity explicitly in this manner is optional but recommended. If you simply post a
comment,view,share or like against a key that does not currently exist, it will be automatically created
for you.
To create an entity, simply call the saveEntity method:
Entity entity = Entity.newInstance("key", "name");
// The "this" argument refers to the current Activity
EntityUtils.saveEntity(this, entity, new EntityAddListener() {
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onCreate(Entity result) {
// Handle success
}
});
Retrieving Entity Data
An existing entity can be retrieved via the getEntity method. Entities obtained in this way will also
provide aggregate data on comments, likes, shares and views. Refer to the Entity Stats (Global) section for more detail on these aggregate values.
// The "this" argument refers to the current Activity
EntityUtils.getEntity(this, "key", new EntityGetListener() {
@Override
public void onGet(Entity result) {
// Handle success
}
@Override
public void onError(SocializeException error) {
if(isNotFoundError(error)) {
// No entity found
}
else {
// Handle error
}
}
});
You can also retrieve several entities in one call:
// The "this" argument refers to the current Activity
EntityUtils.getEntities(this, new EntityListListener() {
@Override
public void onList(ListResult<Entity> result) {
int count = result.getTotalCount();
List<Entity> items = result.getItems();
// Handle success
}
@Override
public void onError(SocializeException error) {
// Handle error
}
}, "key0", "key1");
Retrieving Popular Entities
You can retrieve a list of entities ordered by popularity (activity) by passing a SortOrder paramter to the getEntities method
// Retrieve the top 10 most popular entities
// The "this" argument refers to the current Activity
EntityUtils.getEntities(this, 0, 10, SortOrder.TOTAL_ACTIVITY, new EntityListListener() {
@Override
public void onList(ListResult<Entity> result) {
int count = result.getTotalCount();
List<Entity> items = result.getItems();
// Handle success
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
Entity Stats (Global)
Each entity object retrieved from the server contains aggregate statistics about the number of comments,likes,views and shares:
// The "this" argument refers to the current Activity
EntityUtils.getEntity(this, "key", new EntityGetListener() {
@Override
public void onGet(Entity result) {
// Get the stats
EntityStats entityStats = result.getEntityStats();
entityStats.getLikes();
entityStats.getComments();
entityStats.getViews();
entityStats.getShares();
}
@Override
public void onError(SocializeException error) {
if(isNotFoundError(error)) {
// No entity found
}
else {
// Handle error
}
}
});
Entity Stats (User)
You can also retrieve information about the current user’s actions on the entity:
// The "this" argument refers to the current Activity
EntityUtils.getEntity(this, "key", new EntityGetListener() {
@Override
public void onGet(Entity result) {
// Get the stats
UserEntityStats userEntityStats = result.getUserEntityStats();
userEntityStats.isLiked();
userEntityStats.getComments();
userEntityStats.getShares();
}
@Override
public void onError(SocializeException error) {
if(isNotFoundError(error)) {
// No entity found
}
else {
// Handle error
}
}
});
Entity Activity
All actions performed on an entity (comment,share,like) can be retrieved using ActionUtils
// Get the last 10 actions performed in the app for the entity with the key "key"
ActionUtils.getActionsByEntity(this, "key", 0, 10, new ActionListListener() {
@Override
public void onList(ListResult<SocializeAction> actions) {
// Handle result
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
Users & User Activity
|
Advanced Use
|
The following section is not required if you are simply using the Action Bar
Every device running Socialize has a “User”, however Socialize automatically manages this User for you so there is no need to explicitly create one.
A User has both Settings and Activity. The Settings correspond to the User’s preferences while using Socialize whereas the Activity relates to their social actions.
Getting a User
To obtain a reference to the current user simply call the getCurrentUser method
User currentUser = UserUtils.getCurrentUser(this);
To obtain a User object for a user other than the current user
long id = 123L;
// The "this" argument refers to the current Activity
UserUtils.getUser(this, id, new UserGetListener() {
@Override
public void onGet(User result) {
// Found the user
}
@Override
public void onError(SocializeException error) {
if(isNotFoundError(error)) {
// No such user
}
else {
// Handle error
}
}
});
User Settings
To allow a user to update their settings you can simply present them with the User Settings view:
// The "this" argument refers to the current Activity
UserUtils.showUserSettings(this);
If you want to build your own UI to update a User’s settings you can simply call the saveUserSettings method
// The "this" argument refers to the current Activity
try {
UserSettings userSettings = UserUtils.getUserSettings(this);
UserUtils.saveUserSettings(this, userSettings, new UserSaveListener() {
@Override
public void onUpdate(User result) {
// User was updated
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
}
catch (SocializeException e) {
// Handle error
}
Note
The default comment list activity will launch the default user settings view from a menu option
To globally override the activity that is displayed when a call to showUserSettings is made you can set a user
settings activity class on the global Socialize instance:
// Replace ProfileActivity with your own activity class
Socialize.getSocialize().setUserSettingsActivity(ProfileActivity.class);
This call should be made in the onCreate method of your main activity.
User Activity
You can display a profile view for any user which includes their recent activity
// The "this" argument refers to the current Activity
User user = UserUtils.getCurrentUser(this);
UserUtils.showUserProfile(this, user);
Or retrieve the raw list of actions performed by a user using ActionUtils
User user = UserUtils.getCurrentUser(this);
// Get the most recent 10 actions by this user
// The "this" argument refers to the current Activity
ActionUtils.getActionsByUser(this, user.getId(), 0, 10, new ActionListListener() {
@Override
public void onList(ListResult<SocializeAction> result) {
// Found a result
int totalCount = result.getTotalCount();
List<SocializeAction> items = result.getItems();
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
Or get the list of actions by a user on a single entity
// Get the last 10 actions performed in the app for the entity with the key "key" by the current user
User user = UserUtils.getCurrentUser(this);
ActionUtils.getActionsByUserAndEntity(this, user.getId(), "key", 0, 10, new ActionListListener() {
@Override
public void onList(ListResult<SocializeAction> actions) {
// Handle result
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
Sharing
|
Advanced Use
|
The following section is not required if you are simply using the Action Bar
Socialize provides a complete set of helper functions to make sharing content in your app as easy as possible.
Default Share Dialog
The simplest way to allow users to share an entity (your content) is via the share dialog
Entity entity = Entity.newInstance("http://myentity.com", "My Name");
// The "this" argument refers to the current Activity
ShareUtils.showShareDialog(this, entity);
Custom Share Dialog
If you just want to allow sharing to Social Networks
Entity entity = Entity.newInstance("http://myentity.com", "My Name");
// Setup the options for display
int options = ShareUtils.FACEBOOK | ShareUtils.TWITTER | ShareUtils.GOOGLE_PLUS;
// The "this" argument refers to the current Activity
ShareUtils.showShareDialog(this, entity, new SocialNetworkDialogListener() {
@Override
public void onCancel(Dialog dialog) {
// User cancelled
}
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
// Failed to post to given network
}
}, options);
Adding Text to a Share
If you want to allow the user to add a comment when sharing but you still want to use the default UI controls
you can implement a Flow Interrupt to extract text from the user before the share
Entity entity = Entity.newInstance("http://myentity.com", "My Name");
// The "this" argument refers to the current Activity
ShareUtils.showShareDialog(this, entity, new SocialNetworkDialogListener() {
@Override
public void onShow(Dialog dialog, SharePanelView dialogView) {
// The dialog was shown.
}
@Override
public void onCancel(Dialog dialog) {
// User cancelled.
}
@Override
public void onSimpleShare(ShareType type) {
// User performed a simple share operation (e.g. Email or SMS)
}
@Override
public void onFlowInterrupted(DialogFlowController controller) {
// This will only be called if onContinue returns true
// Obtain share text (e.g. from the user via a dialog)
String text = "Add another message here";
// Call continue when you want flow to resume
controller.onContinue(text);
}
@Override
public boolean onContinue(Dialog dialog, boolean remember, SocialNetwork... networks) {
// Return true if you want to interrupt the flow
return true;
}
});
Customizing the Content of Facebook & Twiter Posts
In some situations you may want to override the default behavior of posting to Facebook or Twitter. In these cases you can
implement the onBeforePost method of the listener you pass to the ShareUtils call
Entity entity = Entity.newInstance("http://myentity.com", "My Name");
// Setup the options for display
int options = ShareUtils.SOCIAL; // This is just a shortcut for Twitter, Facebook and Google+.
// The "this" argument refers to the current Activity
ShareUtils.showShareDialog(this, entity, new SocialNetworkDialogListener() {
@Override
public void onCancel(Dialog dialog) {
// User cancelled
}
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
// Failed to post to given network
}
@Override
public boolean onBeforePost(Activity parent, SocialNetwork socialNetwork, PostData postData) {
// Change data for facebook post
if(socialNetwork != null && socialNetwork.equals(SocialNetwork.FACEBOOK)) {
postData.getPostValues().put("foo", "bar");
}
return false;
}
}, options);
Customizing the Content of Email & SMS Posts
You can also override the default text for email and sms shares
Activity context = this;
Entity entity = Entity.newInstance("http://myentity.com", "My Name");
// Setup the options for display. This can be anything but for this example we will limit to email/sms
int options = ShareUtils.EMAIL | ShareUtils.SMS;
// Display the share dialog with these options
ShareUtils.showShareDialog(context, entity, new SocialNetworkDialogListener() {
@Override
public boolean onBeforePost(Activity parent, SocialNetwork socialNetwork, PostData postData) {
// For email/SMS the socialNetwork will be null but we can change postData
// Possible values are EXTRA_TEXT, EXTRA_SUBJECT and EXTRA_TITLE
postData.getPostValues().put(ShareUtils.EXTRA_TEXT, "This is custom text set in the listener! " + postData.getPropagationInfo().getEntityUrl());
// Return false to indicate we want to continue with the share (i.e. NOT cancel)
return false;
}
}, options);
Retrieving Shares
You can retrieve existing share events by User, Entity, Application or directly using an ID
List shares by User
User user = UserUtils.getCurrentUser(this);
// Get first 10 shares by user
// The "this" argument refers to the current Activity
ShareUtils.getSharesByUser(this, user, 0, 10, new ShareListListener() {
@Override
public void onList(ListResult<Share> result) {
// Found shares
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
List shares by Entity
String entityKey = "http://getsocialize.com";
// Get first 10 shares
// The "this" argument refers to the current Activity
ShareUtils.getSharesByEntity(this, entityKey, 0, 10, new ShareListListener() {
@Override
public void onList(ListResult<Share> result) {
// Found shares
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
List shares by ID
long shareId = 123L;
// The "this" argument refers to the current Activity
ShareUtils.getShare(this, new ShareGetListener() {
@Override
public void onGet(Share result) {
// Share found
}
@Override
public void onError(SocializeException error) {
if(isNotFoundError(error)) {
// No share with ID found
}
else {
// Some other error
}
}
}, shareId);
List shares by Application
// Get first 10 shares
// The "this" argument refers to the current Activity
ShareUtils.getSharesByApplication(this, 0, 10, new ShareListListener() {
@Override
public void onList(ListResult<Share> result) {
// Found shares
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
Likes
|
Advanced Use
|
The following section is not required if you are simply using the Action Bar
Liking
To create a like programmatically you simply call the like method on LikeUtils
Entity entity = Entity.newInstance("http://myentity.com", "My Name");
// The "this" argument refers to the current Activity
LikeUtils.like(this, entity, new LikeAddListener() {
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onCreate(Like result) {
// Like was created
}
});
You can also manually specify how the like is to be propagated to 3rd party networks such as Facebook and Twitter
Entity entity = Entity.newInstance("http://myentity.com", "My Name");
// Get the default options for the user.
LikeOptions likeOptions = LikeUtils.getUserLikeOptions(this);
// Pass the share options in the call to create the like.
// The "this" argument refers to the current Activity
LikeUtils.like(this, entity, likeOptions, new LikeAddListener() {
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onCreate(Like result) {
// Like was created
}
@Override
public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
// Failed to share to the given network
}
@Override
public boolean onBeforePost(Activity parent, SocialNetwork socialNetwork, PostData postData) {
// Called before the post to the given network is made
// Return true to prevent the post from occurring
return false;
}
@Override
public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
// Called after the post to the given network is made
}
@Override
public void onCancel() {
// Called if the user canceled the auth process for social networks
}
}, SocialNetwork.FACEBOOK, SocialNetwork.TWITTER); // Share to multiple networks simultaneously
Un-Liking
You can also remove a previous like from an entity
// Just use the entity key of the entity that was liked
// The "this" argument refers to the current Activity
LikeUtils.unlike(this, "http://myentity.com", new LikeDeleteListener() {
@Override
public void onError(SocializeException error) {
// Handle error
}
@Override
public void onDelete() {
// Like was deleted
}
});
Retrieving Likes
You can retrieve existing likes by User, Entity or Application wide
List likes by User
User user = UserUtils.getCurrentUser(this);
// Get first 10 likes by user
// The "this" argument refers to the current Activity
LikeUtils.getLikesByUser(this, user, 0, 10, new LikeListListener() {
@Override
public void onList(List<Like> likes, int totalCount) {
// Found likes
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
List likes by Entity
String entityKey = "http://getsocialize.com";
// Get first 10 likes
// The "this" argument refers to the current Activity
LikeUtils.getLikesByEntity(this, entityKey, 0, 10, new LikeListListener() {
@Override
public void onList(List<Like> likes, int totalCount) {
// Found likes
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
List likes by Application
// Get first 10 likes
// The "this" argument refers to the current Activity
LikeUtils.getLikesByApplication(this, 0, 10, new LikeListListener() {
@Override
public void onList(List<Like> likes, int totalCount) {
// Found likes
}
@Override
public void onError(SocializeException error) {
// Handle error
}
});
Socialize Core SDK Guide¶
Introduction¶
The Socialize SDK provides a simple set of classes and methods built upon the Socialize REST API
App developers can elect to use either the pre-defined user interface controls provided in the Socialize UI framework, or “roll their own” using direct SDK calls.
ALL calls to the Socialize SDK are asynchronous, meaning that your application will not “block” while waiting for a response from the Socialize server.
You are notified of the outcome of calls to the Socialize service via a SocializeListener passed into each call to the Socialize SDK.
Initializing Socialize (Optional)¶
Note
Socialize will automatically initialize itself upon first use, however if you want more direct control over initialization you can initialize Socialize manually.
The Socialize SDK should be initialized in the onCreate() method of your Activity. You should also include the onPause and onResume methods.
If you want to know about initialization success (e.g. if you want to access Socialize after init), you can also specify a listener
Entities¶
An entity is a single item of content in your app
Throughout the documentation and the code snippets we refer to an “entity”. This is simply a generic term for something that can be view, shared, liked or commented on. Generally this will correspond to a single item of content in your app.
Entities in Socialize MUST be associated with a unique key. It is recommended that where possible an HTTP URL be used (i.e. one that corresponds to an active web page).
Using Entities¶
When posting a comment, share or like using Socialize an entity object must be provided. This can either be an entity you created previously, or a new entity which will be created inline with the call to Socialize.
Note
You should always specify a name when creating an entity, however if you have previously created the entity with a name and do not want to specify the name every time you can provide a “null” argument for the name. This will instruct Socialize to ignore the name attribute and the name of the entity will NOT be updated.
Creating an Entity Inline¶
A typical use of an entity might be when posting a comment. Here the entity name is specified.
Alternatively a null argument can be specified for the name which will instruct the server to leave the existing name intact.
Creating an Entity Explicitly¶
An entity consists of a key and a name. The name should be descriptive and help you identify the entity when viewing reports on the Socialize dash board.
Creating an entity explicitly in this manner is optional but recommended. If you simply post a comment,view,share or like against a key that does not currently exist, it will be automatically created for you.
To create an entity, simply call the saveEntity method:
Retrieving Entity Data¶
An existing entity can be retrieved via the getEntity method. Entities obtained in this way will also provide aggregate data on comments, likes, shares and views. Refer to the Entity Stats (Global) section for more detail on these aggregate values.
You can also retrieve several entities in one call:
Retrieving Popular Entities¶
You can retrieve a list of entities ordered by popularity (activity) by passing a SortOrder paramter to the getEntities method
Entity Stats (Global)¶
Each entity object retrieved from the server contains aggregate statistics about the number of comments,likes,views and shares:
Entity Stats (User)¶
You can also retrieve information about the current user’s actions on the entity:
Entity Meta Data¶
Entities stored in Socialize can optionally include meta data. This is for any additional information you want to store about the entity.
Meta data is stored with the entity and returned then the entity is requested.
If you want a more complex data structure, we recommend using JSON as an object notation
Entity Activity¶
All actions performed on an entity (comment,share,like) can be retrieved using ActionUtils
Users & User Activity¶
Every device running Socialize has a “User”, however Socialize automatically manages this User for you so there is no need to explicitly create one.
A User has both Settings and Activity. The Settings correspond to the User’s preferences while using Socialize whereas the Activity relates to their social actions.
Getting a User¶
To obtain a reference to the current user simply call the getCurrentUser method
To obtain a User object for a user other than the current user
User Settings¶
To allow a user to update their settings you can simply present them with the User Settings view:
If you want to build your own UI to update a User’s settings you can simply call the saveUserSettings method
Note
The default comment list activity will launch the default user settings view from a menu option
To globally override the activity that is displayed when a call to showUserSettings is made you can set a user settings activity class on the global Socialize instance:
This call should be made in the onCreate method of your main activity.
User Activity¶
You can display a profile view for any user which includes their recent activity
Or retrieve the raw list of actions performed by a user using ActionUtils
Or get the list of actions by a user on a single entity
Application Activity¶
Using the ActionUtils class you can also retrieve application-wide activity
Sharing¶
Socialize provides a complete set of helper functions to make sharing content in your app as easy as possible.
Default Share Dialog¶
The simplest way to allow users to share an entity (your content) is via the share dialog
Custom Share Dialog¶
If you just want to allow sharing to Social Networks
Adding Text to a Share¶
If you want to allow the user to add a comment when sharing but you still want to use the default UI controls you can implement a Flow Interrupt to extract text from the user before the share
Customizing the Content of Facebook & Twiter Posts¶
In some situations you may want to override the default behavior of posting to Facebook or Twitter. In these cases you can implement the onBeforePost method of the listener you pass to the ShareUtils call
Customizing the Content of Email & SMS Posts¶
You can also override the default text for email and sms shares
Retrieving Shares¶
You can retrieve existing share events by User, Entity, Application or directly using an ID
List shares by User
List shares by Entity
List shares by ID
List shares by Application
Comments¶
Displaying the Comment List UI¶
The standard Socialize Comment List UI is included with the Socialize Action Bar however if you wanted to create your own ActionBar or simply want to launch the Comment List from elsewhere in your app this can simply be done with a few lines of code
You can also display the UI with a listener to handle events
Making Hyerlinks Clickable¶
The Socialize SDK includes a pre-built OnCommentViewActionListener instance that automatically makes hyperlinks appearing in comments clickable
Adding Comments¶
To create a comment programmatically you simply call the addComment method on CommentUtils
You can also manually specify how the comment is to be propagated to 3rd party networks such as Facebook and Twitter
Adding Comments Without Sharing¶
If you want to explicitly disable sharing of comments you can do this using the CommentOptions
Retrieving Comments¶
You can retrieve existing comments by User, Entity, Application or directly using an ID
List comments by User
List comments by Entity
List comments by ID
List comments by Application
Likes¶
Liking¶
To create a like programmatically you simply call the like method on LikeUtils
You can also manually specify how the like is to be propagated to 3rd party networks such as Facebook and Twitter
Un-Liking¶
You can also remove a previous like from an entity
Retrieving Likes¶
You can retrieve existing likes by User, Entity or Application wide
List likes by User
List likes by Entity
List likes by Application
Views¶
A “view” represents the action of a user viewing an entity and is an extremely useful analytics tool
Recording Views¶