是一些很簡單的東西,以前遇到問題也都搜索求助別人搞定了,現在來做一下整理
下載SDK
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/2011/12/24/Android新浪微博-項目整理總結/weibo4android/
測試
將weibo4android-1.2.1.zip解壓,導入Eclipse,測試運行
|--修改Weibo.java
public static String CONSUMER_KEY = "自己應用的CONSUMER_KEY";
public static String CONSUMER_SECRET = "自己應用的CONSUMER_SECRET ";
|-運行項目
|-打開Android系統自帶的浏覽器進行Oauth認證
|-獲取到AccessToken的key和secret
創建Android項目
|-復制weibo4android,weibo4android.http;weibo4android.org.json;weibo4android.util;下的類都復制到我們新建項目中
|-把commons-codec.jar,commons-httpclient-3.1.jar,commons-logging-1.1.jar這幾個jar也Add To Path到新建項中
傳統的使用Android系統浏覽器進行Oauth認證-參照weibo4android.androidexamples
|-在AndroidManifest.xml中配置授權回調的Activity,以及各種權限別忘了
- <activity android:name=".OAuthActivity">
- <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="Tree" android:host="Growing" />
- </intent-filter>
- </activity>
|-MainActivity
- package tree.love.weiboFinishing;
-
- import weibo4android.Weibo;
- import weibo4android.WeiboException;
- import weibo4android.http.RequestToken;
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
-
- public class MainActivity extends Activity implements OnClickListener
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViewById(R.id.buttonTraditionOAuth).setOnClickListener(this);
- System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
- System.setProperty("weibo4j.oauth.consumerSecret",Weibo.CONSUMER_SECRET);
- }
- @Override
- public void onClick(View v)
- {
- switch (v.getId())
- {
- case R.id.buttonTraditionOAuth:
- Weibo weibo = new Weibo();
- RequestToken requestToken;
- try
- {
- requestToken = weibo.getOAuthRequestToken("tree://Growing");
- OAuthConstant.getInstance().setRequestToken(requestToken);
- Uri uri = Uri.parse(requestToken.getAuthenticationURL());
- startActivity(new Intent(Intent.ACTION_VIEW, uri));
- }
- catch (WeiboException e)
- {e.printStackTrace();}
- break;
- default:
- break;
- }
- }
- }
|-OAuthActivity
- package tree.love.weiboFinishing;
-
- import java.util.List;
-
- import weibo4android.Paging;
- import weibo4android.Status;
- import weibo4android.Weibo;
- import weibo4android.WeiboException;
- import weibo4android.http.AccessToken;
- import weibo4android.http.RequestToken;
- import android.app.Activity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class OAuthActivity extends Activity
- {
-
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.timeline);
- Uri uri = this.getIntent().getData();
- try
- {
- RequestToken requestToken = OAuthConstant.getInstance().getRequestToken();
- AccessToken accessToken = requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
- OAuthConstant.getInstance().setAccessToken(accessToken);
- TextView textView = (TextView) findViewById(R.id.TextView01);
- textView.setText("得到AccessToken的key和Secret,可以使用這兩個參數進行授權登錄了.\n Access token:\n"
- + accessToken.getToken()
- + "\n Access token secret:\n"
- + accessToken.getTokenSecret());
- }
- catch (WeiboException e)
- {
- e.printStackTrace();
- }
- Button button = (Button) findViewById(R.id.Button01);
- button.setText("某一話題下的微博");
- button.setOnClickListener(new Button.OnClickListener() {
-
- public void onClick(View v)
- {
- Weibo weibo = OAuthConstant.getInstance().getWeibo();
- weibo.setToken(OAuthConstant.getInstance().getToken(),
- OAuthConstant.getInstance().getTokenSecret());
- List<Status> friendsTimeline;
- try
- {
- friendsTimeline = weibo.getTrendStatus("seaeast",
- new Paging(1, 20));
- StringBuilder stringBuilder = new StringBuilder("");
- for (Status status : friendsTimeline)
- {
- stringBuilder.append(status.getUser().getScreenName()
- + "說:\n"
- + status.getText()
- + "\n--------------------------------------------------\n");
- }
- TextView textView = (TextView) findViewById(R.id.TextView01);
- textView.setText(stringBuilder.toString());
- }
- catch (WeiboException e)
- {
- e.printStackTrace();
- }
- }
- });
- }
- }
|-OAuthConstant
- package tree.love.weiboFinishing;
-
- import weibo4android.Weibo;
- import weibo4android.http.AccessToken;
- import weibo4android.http.RequestToken;
-
- public class OAuthConstant
- {
- private static Weibo weibo = null;
- private static OAuthConstant instance = null;
- private RequestToken requestToken;
- private AccessToken accessToken;
- private String token;
- private String tokenSecret;
-
- private OAuthConstant()
- {
- };
-
- public static synchronized OAuthConstant getInstance()
- {
- if (instance == null)
- instance = new OAuthConstant();
- return instance;
- }
-
- public Weibo getWeibo()
- {
- if (weibo == null)
- weibo = new Weibo();
- return weibo;
- }
-
- public AccessToken getAccessToken()
- {
- return accessToken;
- }
-
- public void setAccessToken(AccessToken accessToken)
- {
- this.accessToken = accessToken;
- this.token = accessToken.getToken();
- this.tokenSecret = accessToken.getTokenSecret();
- }
-
- public RequestToken getRequestToken()
- {
- return requestToken;
- }
-
- public void setRequestToken(RequestToken requestToken)
- {
- this.requestToken = requestToken;
- }
-
- public String getToken()
- {
- return token;
- }
-
- public void setToken(String token)
- {
- this.token = token;
- }
-
- public String getTokenSecret()
- {
- return tokenSecret;
- }
-
- public void setTokenSecret(String tokenSecret)
- {
- this.tokenSecret = tokenSecret;
- }
- }