Android: Weibo sharing and precautions
In front I wrote two articles about QQ and sharing (http://blog.csdn.net/dawanganban/article/details/42015249) This article we look at how to achieve microblogging sharing function.
Weibo's reference documents have been wandering around the open platform for a long time. If they are not found, they are surprised to find that in the SDK Demo, they are still my old ideas, the fastest and best start point for third-party access is the Getting Started documentation and official Demo. When opening the Demo, we will see the relevant documentation and source code. Because the previous APPID application is similar to QQ, so I won't repeat it here. You can refer to the above two articles. Next, I will stick out the encapsulated Source Code as follows:
Public class SinaShareManager {/*** text */public static final int sina_gate_way_text = 1;/*** image */public static final int sina_gate_way_pic = 2; /*** link */public static final int sina_gate_way_webpage = 3; private static String sinaAppKey; public static final String SCOPE = "email, direct_messages_read, direct_messages_write," + "begin, complete, statuses_to_me_read, "+" Follow_app_official_microblog, "+" invitation_write ";/** Weibo sharing interface instance */private iweibow.api sinaAPI; public void registSina (Context context) {// obtain appkey if (sinaAppKey = null) {sinaAppKey = SinaShareUtil. getSinaAppKey (context);} // initialize the Weibo sharing code if (sinaAppKey! = Null) {initSinaShare (context) ;}/ *** Sina Weibo Sharing Method * @ param shareContent Content shared content */public void shareBySina (plain content into content, Context context) {if (sinaAPI = null) return; switch (Response content. getgateway () {case sina_gate_way_text: shareText (publish content); break; case sina_gate_way_pic: sharePicture (publish content, context); break; case when: publish webpage (publish content, context); bre Ak ;}/ ** share text */private void shareText (publish content into content) {// initialize WeiboMessage weiboMessage = new WeiboMessage (); weiboMessage. mediaObject = getTextObj (Response content. getContent (); // initialize the message request from a third party to Weibo sendmessagetoweibo orequest request = new sendmessagetoweibo orequest (); request. transaction = buildTransaction ("sinatext"); request. message = weiboMessage; // send the request information to Weibo, arousing the sinaAPI on the Weibo sharing interface. sendRequest (re Quest);}/** share image */private void sharePicture (custom content into content, Context context) {WeiboMessage weiboMessage = new WeiboMessage (); weiboMessage. mediaObject = getImageObj (invalid content. getPicResource (), context); // initialize a message request from a third party to Weibo: sendmessagetoweibo orequest request = new sendmessagetoweibo orequest (); request. transaction = buildTransaction ("sinatext"); request. message = weiboMessage; // send the request message to Weibo to arouse the score Interface sinaAPI. sendRequest (request);} private void response webpage (Response content containing content, Context context) {WeiboMessage weiboMessage = new WeiboMessage (); weiboMessage. mediaObject = getWebpageObj (Response content, context); // initialize the sendmessagetoweibo request from a third party to a Weibo message request = new sendmessagetoweibo orequest (); request. transaction = buildTransaction ("sinatext"); request. message = weiboMessage; // send request information to Weibo to arouse the sharing community SinaAPI. sendRequest (request);}/*** welcome-Sunshine Xiaoqiang-http://blog.csdn.net/dawanganban * @ author lixiaoqiang **/private abstract class using content {protected abstract int getusing way (); protected abstract String getContent (); protected abstract String getTitle (); protected abstract String getURL (); protected abstract int getPicResource ();} /*** set the content of shared text * @ author Administrator **/public class internal content Text extends plain content {private String content;/*** construct the shared text class * @ param Text shared text content */public plain contenttext (String content) {this. content = content ;}@ Overrideprotected String getContent () {return content ;}@ Overrideprotected String getTitle () {return null ;}@ Overrideprotected String getURL () {return null ;} @ Overrideprotected int getPicResource () {return-1 ;}@ Overrideprotected int getgateway () {ret Urn sina_gate_way_text ;}}/*** set the content of the shared image * @ author Administrator **/public class internal contentpic extends content {private int picResource; public extends contentpic (int picResource) {this. picResource = picResource ;}@ Overrideprotected String getContent () {return null ;}@ Overrideprotected String getTitle () {return null ;}@ Overrideprotected String getURL () {return null ;} @ Overrideprotected int getPicRe Source () {return picResource;} @ Overrideprotected int getgateway () {return sina_gate_way_pic ;}} /*** set the content of the Sharing link * @ author Administrator **/public class internal contentwebpage extends plain content {private String title; private String content; private String url; private int picResource; public writable contentwebpage (String title, String content, String url, int picResource) {this. title = title; this. content = Content; this. url = url; this. picResource = picResource ;}@ Overrideprotected String getContent () {return content ;}@ Overrideprotected String getTitle () {return title ;}@ Overrideprotected String getURL () {return url ;} @ Overrideprotected int getPicResource () {return picResource;} @ Overrideprotected int getgateway () {return sina_gate_way_webpage;}/*** creates a text message object. ** @ Return refers to the text message object. */Private TextObject getTextObj (String text) {TextObject textObject = new TextObject (); textObject. text = text; return textObject;} private ImageObject getImageObj (int picResource, Context context) {ImageObject imageObject = new ImageObject (); Bitmap bmp = BitmapFactory. decodeResource (context. getResources (), picResource); imageObject. setImageObject (bmp); return imageObject;} private WebpageObj Ect getWebpageObj (Response content contains content, Context context) {WebpageObject mediaObject = new WebpageObject (); mediaObject. identify = Utility. generateGUID (); mediaObject. title = Invalid Content. getTitle (); mediaObject. description = Invalid Content. getContent (); // set Bitmap images to video objects. Bitmap bmp = BitmapFactory. decodeResource (context. getResources (), response content. getPicResource (); mediaObject. setThumb Image (bmp); mediaObject. actionUrl = Response content. getURL (); mediaObject. defaultText = Invalid Content. getContent (); return mediaObject;} private void initSinaShare (Context context) {// create a Weibo SDK interface instance sinaAPI = WeiboShareSDK. createWeiboAPI (context, sinaAppKey); // check version support checkSinaVersin (context); sinaAPI. registerApp ();} private void checkSinaVersin (final Context context) {// obtain Weibo client information, such as whether to install or support the SDK version Boolean isInstalledWeibo = sinaAPI. isWeiboAppInstalled (); // int supportApiLevel = sinaAPI. getWeiboAppSupportAPI (); // if the Weibo client is not installed, set the callback if (! IsInstalledWeibo) {sinaAPI. registerWeiboDownloadListener (new IWeiboDownloadListener () {@ Override public void onCancel () {Toast. makeText (context, "cancel download", Toast. LENGTH_SHORT ). show () ;}) ;}} private String buildTransaction (final String type) {return (type = null )? String. valueOf (System. currentTimeMillis (): type + System. currentTimeMillis ();}}If you have any questions, you can ask or add a QQ group next to your blog to communicate. I hope these three articles will be helpful to you.