Weibo development (4)-saving certification information, menu bar, and Weibo Homepage

Source: Internet
Author: User

Preface

I haven't updated my blog for many days. I 've been busy studying tutorials and getting stuck by some knowledge points these days. Today, I finally achieved the welcome page and authenticated logon. The menu bar at the bottom is simple and the Weibo homepage is simple, in addition, the accesstoken information after authentication is saved to avoid repeated authentication every time.

I personally think that I want to complete this section, and learn the following knowledge points carefully:

1. which method is used in the menu bar and what is tabhost?

2. layout, including component placement and "Switching Effect" using XML, such as style changes before and after clicking the "button"

3. The version of Sina SDK directly affects the use of various objects that need to be created by yourself.

4. understand some objects in Weibo, such as Weibo, user, status, accesstoken, etc,

5. listview and adapter Problems

6. Clarify the ideas and procedures, including when to authenticate and log on, where to save information, and whether to create or obtain instance references.

First look:

 

Implementation

After studying the source code obtained by Sina Weibo and Svn of many versions on the Internet, I fainted my weibo development. At one time I did not know how to get the next step. I am told by practice, without understanding how terrible the framework is.

Although some functions are implemented, the structure is not very good and can be optimized in many places;

1. Save the accesstoken after the first Authentication

It is very easy to save the authentication information. I use sharedpreferences to save the token and secret in the accesstoken object obtained by the authentication. The following getaccesstokenfirst () method is customized by me, this function is used to generate an accesstoken (obtaining the requesttoken has been implemented before and is no longer used) and is saved as follows:

Private void getaccesstokenfirst () throws throwable
{
Uri uri = This. getintent (). getdata ();
String oauth_verifier = URI. getqueryparameter ("oauth_verifier ");
Mainservice. mweibo O. addoauthverifier (oauth_verifier );
// Get the accesstoken and assign it to Weibo in the generateaccesstoken () function.
Mainservice. mweibo O. generateaccesen en (this, null );

Sharedpreferences preferences = getsharedpreferences ("userauthinfor", context. mode_private );
Editor editor = preferences. Edit ();
// Write content to the parameter file using the editor Method
Editor. putstring ("access_token_token", mainservice. mweibo. getaccesen en (). gettoken ());
Editor. putstring ("access_token_secret", mainservice. mweibo. getaccesen en (). getsecret ());
// You must submit the file before it can be saved.

Editor. Commit ();
}

 

Now that the first authentication is complete, you can use the client to avoid Logon (No Logon during the validity period). You can determine whether it is necessary before obtaining the requesttoken. The specific idea is, retrieve the content saved by sharedpreferences from the local device. If you have already authenticated the content, you can obtain the token and secret that are not empty. If not, you will authenticate the content for the first time:

    if (CheckAccessToken() != null)
{
Log.i(TAG, "---------CheckAccessToken() != null");
MainService.mWeibo.setAccessToken(CheckAccessToken());
Log.i(TAG, "token:" + MainService.mWeibo.getAccessToken().getToken() + "-------secret:"
+ MainService.mWeibo.getAccessToken().getSecret());
Intent it = new Intent(AuthorizeActivity.this, MainActivity.class);
startActivity(it);
finish();

} else
{
Log.i(TAG, "---------AuthorizeNow()");
setContentView(R.layout.main);
AuthorizeNow();

}

In this Code, the checkaccesstoken () is used to check whether authentication information is available. The return value is accesstoken or NULL:

// Check whether authorization information exists in sharedpreferences.
Public accesstoken checkaccesstoken ()
{
Accesstoken = NULL;
Sharedpreferences preferences = getsharedpreferences ("userauthinfor", context. mode_private );
String token = preferences. getstring ("access_token_token", null );
String secret = preferences. getstring ("access_token_secret", null );
If (token = NULL | secret = NULL)
{
Accesstoken = NULL;
} Else
{
Accesstoken = new accesstoken (token, secret );
}
Return accesstoken;
}

 

Authorizenow () is of course authenticated. On the Sina authentication page, the user enters the password and User Name:

    public void AuthorizeNow()
{
this.oauthNote = (TextView) this.findViewById(R.id.tvToken);
this.oauthBtn = (Button) this.findViewById(R.id.loginButton);
this.oauthBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == oauthBtn)
{
MainService.mWeibo = Weibo.getInstance();
MainService.mWeibo.setupConsumerConfig(CONSUMER_KEY, CONSUMER_SECRET);
try
{
RequestToken requestToken = MainService.mWeibo.getRequestToken(
AuthorizeActivity.this, Weibo.APP_KEY, Weibo.APP_SECRET,
AuthorizeActivity.URL_ACTIVITY_CALLBACK);
oauthNote.setText(requestToken.getToken());
Uri uri = Uri.parse(Weibo.URL_AUTHENTICATION
+ "?display=wap2.0&oauth_token=" + requestToken.getToken()
+ "&from=" + AuthorizeActivity.FROM);
Log.i(TAG, "---------jump to browser for AUTHENTICATION");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
finish();
} catch (WeiboException e)
{
e.printStackTrace();
}
}
}
});
}

 

Ii. Implement menu bar

This is complicated. I am also not clear about it. I 'd better first look at the tabhost content in the API documentation. The cases it comes with are clear, and my implementation is changed from there. First run the Code:

Resources res = getresources ();
Tabhost = gettabhost ();
Tabhost. tabspec spec;
Intent intent;
// Home Page
Intent = new intent (). setclass (this, homepageactivity. Class );
Spec = tabhost. newtabspec ("Homepage ")
. Setindicator ("Homepage", res. getdrawable (R. drawable. icon_1). setcontent (intent );
Tabhost. addtab (SPEC );
// Information
Intent = new intent (). setclass (this, msgactivity. Class );
Spec = tabhost. newtabspec ("MSG"). setindicator ("info", res. getdrawable (R. drawable. icon_2 ))
. Setcontent (intent );
Tabhost. addtab (SPEC );
// My documents
Intent = new intent (). setclass (this, userinfoactivity. Class );
Spec = tabhost. newtabspec ("2"). setindicator ("My documents", res. getdrawable (R. drawable. icon_3 ))
. Setcontent (intent );
Tabhost. addtab (SPEC );
// Search
Intent = new intent (). setclass (this, searchactivity. Class );
Spec = tabhost. newtabspec ("3"). setindicator ("Search", res. getdrawable (R. drawable. icon_4 ))
. Setcontent (intent );
Tabhost. addtab (SPEC );

// More
Intent = new intent (). setclass (this, moreitemsactivity. Class );
Spec = tabhost. newtabspec ("3"). setindicator ("more", res. getdrawable (R. drawable. icon_5 ))
. Setcontent (intent );
Tabhost. addtab (SPEC );
// Display the Home Page
Tabhost. setcurrenttab (0 );

 

The above is simply to set an activity for each entry in the menu. They need to create an activity. For example, the first homepageactivity is my weibo homepage activity, where no button is used for activity redirection. If you need to layout the file, you will not mention it. You can use the official case to modify it.

3. Show friends on the homepage

The Avatar function is not complete yet. Now, only the "nickname" + "content" of the target is displayed ". The code in the home page homepageactivity uses listview to bind the adapter.

Public listview allstatus;

// Myadapter is a custom adapter that inherits the parent class baseadapter

Myadapter MA = new myadapter (this, status );
Log. I (TAG, "--------- new myadapter (this, status );");
Allstatus. setadapter (MA );
Log. I (TAG, "--------- allstatus. setadapter (MA );");

 

The key is not to be bound here. The implementation of the myadapter class is the key. debugging of code errors is basically a problem here. Rewrite the getview () method, bind the style file used as the listview for each item.

 

      
@ Override
Public View getview (INT position, view convertview, viewgroup parent)
{
View statusview = NULL;
If (convertview! = NULL) & (convertview. findviewbyid (R. Id. ivitemportrait )! = NULL ))
{
Log. D ("listview", "Do getview" + Position + "getoldtextview ");
// Obtain the entry information stored in the original memory
Statusview = convertview;
} Else
{
Log. D ("listview", "Do getview" + Position + "newtextview ");
Statusview = layoutinflater. From (context). Inflate (R. layout. itemview, null );
Log. D ("listview", "After do getview" + Position + "newtextview ");
}

// Set the content displayed for this entry
Viewholder holder = NULL;
Holder = new viewholder ();
// Holder. ivitemportrait = (imageview)
// Statusview. findviewbyid (R. Id. ivitemportrait );
Holder. tvitemname = (textview) statusview. findviewbyid (R. Id. tvitemname );
Holder. tvitemcontent = (textview) statusview. findviewbyid (R. Id. tvitemcontent );
// Set the nickname
Holder. tvitemname. settext (all. get (position). getuser (). getname ());
// Set content
Com. Aven. util. textautolink. addurlspan (all. get (position). gettext (),
Holder. tvitemcontent );
// Update the Avatar

Return statusview;
}

 

4. Notes

This is probably the main framework of my code. It should be noted about setting nicknames. After obtaining JSON data from the server, parse the data and encapsulate it into the status pair set, this status has been defined in some sdks, such as weibo4j and weibo4jandroid, but weibo_sdk_source_code_0713 does not, or even many basic classes, therefore, selecting the SDK is also very important (for beginners who have little experience). At the beginning, I used weibo_sdk_source_code_0713, which is very troublesome because of the tutorial, none of the materials are in this version.

There is also a problem about menu bar loss. When switching between different activities, sometimes the following menu will disappear. This goes through Baidu and can be referenced in two places. One is in the Sina Blog, I have summarized the two methods. I only solved the problem after reading her blog post, and I have asked a garden friend about the article "farmer's uncle" in the blog garden, I read it a bit similar and asked him for advice, but I don't know if I have replied yet.

Summary

The Sina Weibo client model has come out, and the rest is to improve other pages, as well as image display and code optimization. In this article, I would like to ask you to point out that you are welcome to share your ideas.

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.