Android shares with Sina Weibo, authenticates and sends Weibo messages,

Source: Internet
Author: User
Tags oauth

I shared my time on Sina Weibo for more than half a month. Now I finally got it. It's so nice. Haha !!

To put it bluntly, first authentication is required,

1. Enter the open platform http://open.weibo.com/registered Sina account provided by Sina Weibo.

2. Click "I Am a developer" to create an application, get the CONSUMER_KEY and CONSUMER_SECRET, and save the two values.

3. Find the relevant API documentation, download the android SDK weibo4android-1.2.0.zip.

4. decompress the package and import it to eclipse.

------ Below are the key steps

5. Create a lib folder in your project and add the three package commons-codec.jar, commons-httpclient-3.1.jar, and commons-logging-1.1.jar in the weiboandroid project, respectively.

6. Add the four packages to your project, including weibo4android, weibo4android. http, weibo4android.org. json, and weibo4android. util. // At this time if the introduction of weibo4android-1.2.0.jar this package will report an error, prompting what version problems, the query provides is from version 1.6 copy project to Version 2.2 solution. In my opinion, this package conflicts with the four packages that have been poured into the namespace.

7. Refer to the three files under the examples-weibo4android.androidexamples. OAuthConstant. java directly copies the configuration required for AndroidExample. java authentication to the project. After the OAuthActivity. java authentication is successful, the AccessToken key and Secret are obtained and the code can be copied. // Detailed code settings will be provided later. Don't worry ..................

8. java files corresponding to AndroidExample and OAuthActivity code must be registered in AndroidManifest. xml. Not to mention the registration method, basic entry required course.

Note:

<Intent-filter>
<Action android: name = "android. intent. action. VIEW"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Category android: name = "android. intent. category. BROWSABLE"/>
<Data android: scheme = "cn.edu. cou. mobile" android: host = "ShareSinaSendActivity"/> //
</Intent-filter>

9. The most important thing is to find the Weibo. java file under the weibo4android package copied to the project and modify the values of CONSUMER_KEY and CONSUMER_SECRET, which correspond to the values mentioned in the second point.

10. Code:

AndroidExample files must include:

System. setProperty ("weibo4j. oauth. consumerKey", Weibo. CONSUMER_KEY );
System. setProperty ("weibo4j. oauth. consumerSecret", Weibo. CONSUMER_SECRET );

Then:

Weibo weibo = OAuthConstant. getInstance (). getWeibo ();
RequestToken requestToken;
Try {
RequestToken = weibo O. getOAuthRequestToken ("weibo4android: // OAuthActivity"); // obtain the scheme host mentioned in the eighth point, respectively. The host must be the name of the page to be called back after Sina succeeds.
Uri uri = Uri. parse (requestToken. getAuthenticationURL () + "& from = xweibo"); // the corresponding file of OAuthActivity. It is recommended that scheme be the project name or the total namespace of the project.
OAuthConstant. getInstance (). setRequestToken (requestToken );
StartActivity (new Intent (Intent. ACTION_VIEW, uri ));
} Catch (WeiboException e ){
E. printStackTrace ();

}

OAuthActivity file:

Uri uri = this. getIntent (). getData ();
Try {
RequestToken requestToken = OAuthConstant. getInstance ()
. GetRequestToken ();
AccessToken = requestToken. getAccessToken (uri
. GetQueryParameter ("oauth_verifier "));
OAuthConstant. getInstance (). setAccessToken (accessToken );
TextView. setText ("Get the key and Secret of the AccessToken. You can use these two parameters to authorize logon. \ n Access token: \ n"
+ AccessToken. getToken ()
+ "\ N Access token secret: \ n"
+ AccessToken. getTokenSecret ());
} Catch (WeiboException e ){
E. printStackTrace ();
}

//// ---------------- The following describes how to process and publish a new microblog In The OnClick event of the Button in the corresponding OAuthActivity file.

Weibo weibo = new Weibo ();
Status status = null;
Try {
Weibo. setToken (accessToken. getToken (), accessToken. getTokenSecret ());
Status = weibo. updateStatus ("test Sina weibo message on android !!!! "); // The key here is whether you can successfully call updateStatus to post a microblog. If you are interested, please take a closer look.
} Catch (WeiboException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println (status. getId () + ":" + status. getText () + ""
+ Status. getCreatedAt ());

// If the program can be executed here, it indicates that the statement has been published successfully. The following shows how to jump to another page after the execution is successful.

Intent intent = new Intent ();
Intent. setClass (ShareSinaSendActivity. this, ShareListActivity. class );
ShareSinaSendActivity. this. startActivity (intent );

At this point, the function is basically complete. If you want other functions, you only need to perform corresponding operations based on the Demo provided by the SDK.

Conclusion: to share it with Sina Weibo and call Sina's API, some xml layout files are required in addition to the interface provided by Sina. I am a beginner. The above is a record of the successful sharing. I hope it can help many people who need to share Sina Weibo. For the knowledge of android, each person provides more experience, which is both a kind of improvement for himself and can help more people. It is really good !!!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.