Android calls Netease Weibo open API

Source: Internet
Author: User
Tags oauth

Today, I tried to use Netease Weibo's open platform to develop applications on Android. After the SDK for Java is downloaded, there are still some problems to use. But it was solved later.
Next we will record the entire process to some friends who need it.

1, to http://open.t.163.com/wiki/index.php? Title = SDK # download the SDK from Java. We noticed that the SDK package isSource code.

2. Create an application in the http://open.t.163.com/apps/new to get the consumerkey and consumersecret of the application.

3. Create an android project using eclipse. Note that the Android version must be 2. x. Put the content in the SRC folder of the SDK returned in step 1 to the src directory of the android project.

4. Configure buildpath and add the jar package under Lib in the SDK to the android project. (I will not elaborate on how to configure buildpath)

5. After completing these steps, the httpclient class still reports an error. javax. Activation. mimetypesfiletypemap cannot be found. At this moment, go to the http://www.jar114.com/this website to search for javax. activation. in which package is mimetypesfiletypemap, download the package back, configure buildpath, and add the downloaded package to the project. In this case, no error is reported.

6. In the SDK,ProgramThe entry is the oauthgettoken class in the example package, but now we are an android application. The entry is in activity, therefore, we copied the methods of the main method in the oauthgettoken class to the oncreate method of our own activity in the android project.

7. CopyCode...
System. setproperty ("tblog4j. oauth. consumerkey ",
"");
System. setproperty ("tblog4j. oauth. consumersecret ",
"");
Set the consumerkey and consumersecret obtained in the second step.

8. Add Internet access permission settings in the androidmanifest file.
<Uses-Permission Android: Name = "android. Permission. Internet"> </uses-Permission>

9. It looks similar. Run a bunch of errors. I found the cause and found that the property file is used in the program. Now the file is in the SRC folder, but in Android, the property file cannot be read here. So put the t4j. properties file under SRC into assets. In this case, we need to change the access path.

10. Find 81 lines of the configuration class under the t4j package.

Load properties (defaultproperty, configuration. Class. getresourceasstream ("/" + t4jprops ));

Changed to loadproperties (defaultproperty, configuration. Class. getresourceasstream ("/assets/" + t4jprops ));

11. At this time, let's take a look at the process of calling methods.

The first step is to generate a URL to verify our application. It is easy to think that this URL carries the Application ID information.

Step 2: Call the browser to access this URL. On the page, set the user to allow our application to access his account. After setting, 163 saves the allowed status.

Step 3: After the configuration is complete, the user exits the browser and returns to our application. Then, our application calls the SDK to get the accesstoken from 163.

Step 4: Use accesstoken to call any method in the SDK, such as verifying users and obtaining Weibo lists.

The result of my example below is that, as soon as you enter the application, click the button to verify. After the verification, the user exits the browser and the application displays the user's Weibo user name.

Code:

Import t4j. tblog; <br/> Import t4j. tblogexception; <br/> Import t4j. data. user; <br/> Import t4j. HTTP. accesstoken; <br/> Import t4j. HTTP. requesttoken; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import Android. widget. textview; </P> <p> public class welcome extends activity implements onclicklistener {<br/> private string verifyurl; <br/> private textview tvtip; <br/> private textview tvusername; <br/> private tblog; <br/> private requesttoken; <br/> // indicates whether the flag has been verified <br/> private int flag = 0; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedin Stancestate); <br/> setcontentview (R. layout. main); </P> <p> tvtip = (textview) findviewbyid (R. id. TV _tip); <br/> tvusername = (textview) findviewbyid (R. id. TV _username); <br/> button btnverify = (button) findviewbyid (R. id. btn_verify); <br/> btnverify. setonclicklistener (this); <br/> // set the application information <br/> system. setproperty ("tblog4j. oauth. consumerkey "," kkyj0scn seo1jsuk "); <br/> system. setproperty ("tblog4j. OAU Th. consumersecret "," isjf44l9ftkzhdrslspi4k22ydfb55e "); <br/> // Disable debug to reduce interference Information <br/> system. setproperty ("tblog4j. debug "," false "); <br/> tvtip. settext ("click the button to go to 163 to verify the application"); <br/> tblog = new tblog (); <br/> // obtain requesttoken <br/> try {<br/> requesttoken = tblog. getoauthrequesttoken (); <br/>} catch (tblogexception E1) {<br/> // todo auto-generated Catch Block <br/> e1.printstacktrace (); <br/>}</P> <p> // Obtain the verification URL <br/> verifyurl = requesttoken. getauthenticationurl (); <br/>}</P> <p> // onresume is executed when the browser is closed and returned to the application. <Br/> // because onresume is a method that is often executed, set the flag Flag to ensure that the accesstocken is obtained only when verification is returned. <br/> protected void onresume () {<br/> super. onresume (); <br/> If (flag = 1) {<br/> tvtip. settext ("show user name after verification"); <br/> accesstoken accesstocken = NULL; <br/> try {<br/> accesstocken = tblog. getoauthaccesstoken (requesttoken); <br/>} catch (tblogexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< /P> <p> // you can save the accesstocken and do not need to obtain it again later. <Br/> tblog. settoken (accesstocken. gettoken (), accesstocken. gettokensecret (); </P> <p> // obtain Weibo user information <br/> User user = NULL; <br/> try {<br/> User = tblog. verifycredentials (); <br/>} catch (tblogexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> tvusername. settext (user. getname (); <br/> flag = 2; <br/>}</P> <p> @ override <br/> Public void onclick (view arg0) {</P> <p> // call the browser to open the URL <br/> intent = new intent (); <br/> intent. setaction ("android. intent. action. view "); <br/> URI content_uri_browsers = Uri. parse (verifyurl); <br/> intent. setdata (content_uri_browsers); <br/> intent. setclassname ("com. android. browser "," com. android. browser. browseractivity "); <br/> startactivity (intent); <br/> flag = 1; <br/>}< br/>}

:

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.