-
Notifications
You must be signed in to change notification settings - Fork 156
Interface Principles
Note This article describes the basic principles of ShareSDK built-in (no GUI) interfaces. If you want to look up the interfaces of "OnekeyShare" (with GUI), refer to the "OnekeyShare and its customizing".
For most operations of ShareSDK, such as: authorization, obtaining user info, sharing, and so on. There are fixed formats of calling their interfaces, and described by the following pseudo-codes:
- Call the initSDK method
- Access the platform instance
- Add a callback instance operations on the platform
- Call the right method of the operation on the platform
No matter where you use ShareSDK, initSDK must be called be any operation. As the result, we strongly recommend you that calling initSDK at the onCreate method in the entrance Activity of your application. In addition, you can call this method repeatly at any time you are unsure of the situation of ShareSDK, without worrying about the loss of efficiency.
To finish the operation, you have to get the platform instance first. There are two ways to get the platform:
// get all platforms registered in ShareSDK
Platform[] platformList = ShareSDK.getPlatformList(context);
// get the platform by its name
Platform plat = ShareSDK.getPlatform(context, TencentWeibo.NAME);
```
# Set Operation Callback
You can start your work after taking the platform instance. However, most of the interfaces of ShareSDK return the void value, if you want to receive the result of the operation; you have to set an instance of PlatformActionListener to be the callback of the operation like the following:
```` Java
Platform weibo = ShareSDK.getPlatform(context, TencentWeibo.NAME);
weibo.setPlatformActionListener(new PlatformActionListener() {
public void onError(Platform platform, int action, Throwable t) {
// handling error message
}
public void onComplete(Platform platform, int action,
HashMap<String, Object> res) {
// handling success result
}
public void onCancel(Platform platform, int action) {
// handling canceled message
}
});
```
In general, the "Cancel" event is rare, besides authorization. The “Platform” parameter is the platform of the operation, “action” is the code of the operation, and you can refer to [the API doc](http://sharesdk.cn/androidDoc/cn/sharesdk/framework/Platform.html) to find the list of these actions. “Throwable” field in “onError” method contains error message, you can **call "t.printStackTrace ()" to print the whole error message stack**. This stack is very important; if you submit bugs to ShareSDK customer service, please bring this stack, so they check exception. and the “HashMap” in the “onComplete” is the operation result, most API of the socialize platforms return JSON format result, ShareSDK parses the response and transfers it into the HashMap.
# Specific Operation
ShareSDK provides authorization, obtaining user info, sharing, getting a list of friends (or followings), and custom interfaces for customizing, but not every platform supports all these methods, for more information of calling these methods, please referring to specific chapters.