创建应用,会得到一个唯一的key和secret,如下图







需要导入除了core.jar外的其他包,如下图









我自己写了一个调用腾讯微博的类 TencentWeiboUtil

public class TencentWeiboUtil{

public static final String fileName = "token_store";



private static OAuth oauth;

private static OAuthClient auth;



private final String KEY = "801115034";

private final String SECRET = "6dfcb2d5e8fe5115f917b136ab7ff6e8";



private String clientIp;



private static TencentWeiboUtilinstance;



private TestActivity mActivity;



private String mOauth_token;



private String mOauth_token_secret;



public static TencentWeiboUtilgetInstance() {

if (instance == null) {

instance = new TencentWeiboUtil();

}

return instance;

}



private TencentWeiboUtil() {

if (oauth == null) {

oauth = new OAuth(KEY, SECRET, "qweibo4android://OAuthActivity"); // 初始化OAuth请求令牌

}

mActivity = AppConst.getInstance().getActivity();

}



public void sendTencentWeibo() {

String[] oauth_token_array = fetch(mActivity);

mOauth_token = oauth_token_array[0];

mOauth_token_secret = oauth_token_array[1];

if (mOauth_token != null && mOauth_token_secret != null) {

oauth.setOauth_token(mOauth_token);

oauth.setOauth_token_secret(mOauth_token_secret);

shareTentcentWeibo();

return;

}

// 获取requesttoken

try {

if (auth == null) {

auth = new OAuthClient();// OAuth 认证对象

}

oauth = auth.requestToken(oauth);

if (oauth.getStatus() == 1) {

System.out.println("Get RequestToken failed!");

return;

} else {

String toauth_token = oauth.getOauth_token();



String url = "http://open.t.qq.com/cgi-bin/authorize?oauth_token="

+ toauth_token;



Log.d("tag=========", "AndroidExampleurl = " + url);



Uri uri = Uri.parse(url);



mActivity.startActivity(new Intent(Intent.ACTION_VIEW, uri));

}

} catch (Exception e) {

// TODO Auto-generatedcatch block

e.printStackTrace();

}

}



/**

* get token from verifiercode

*

* @param oauth_verifier

* @param oauth_token

*/

public void getToken(String oauth_verifier, String oauth_token) {

oauth.setOauth_verifier(oauth_verifier);

oauth.setOauth_token(oauth_token);



clientIp = Configuration.wifiIp;

Log.v("tag============", "clientIp:" + clientIp);



try {

oauth = auth.accessToken(oauth);

} catch (VerifyError err) {

err.printStackTrace();

} catch (Exception e) {

// TODO Auto-generatedcatch block

e.printStackTrace();

}



if (oauth.getStatus() == 2) {

System.out.println("Get AccessToken failed!");

return;

} else {

Log.d("tag===========",

"OAuthActivity Oauth_token : " + oauth.getOauth_token());

Log.d("tag===========", "OAuthActivityOauth_token_secret : "

+ oauth.getOauth_token_secret());



// 已经拿到access token,可以使用oauth对象访问所有API了

// 将accesstoken存储到SharedPreferences里

store(mActivity, oauth);

}



shareTentcentWeibo();

}



private void shareTentcentWeibo() {

T_API tapi = new T_API();



String content = "腾讯微博分享测试";



if (content == "") {

content = "发表微博";

}



try {

String str = tapi.add(oauth, "json", content, clientIp, "", "");

Log.v("tag===============", "result str= " + str);

org.json.JSONTokener ae = null;

ae = new org.json.JSONTokener(str);

try {

JSONObject msg = (JSONObject) ae.nextValue();

int errCode = -1;

if (!msg.isNull("errcode")) {

errCode = msg.getInt("errcode");

}

Message message = new Message();

message.what = AppConst.MSG_RECEVIED_TENCENT_WEIBO_RESULT;

if (errCode == 0) {

message.obj = mActivity.getResources().getString(

R.string.send_sucess);

}else {

String result = "";

if (!msg.isNull("msg")) {

result = msg.getString("msg");

message.obj = result;

}

}

AppConst.getInstance().getUIHandler().sendMessage(message);

} catch (Exception ex) {



}

} catch (Exception e) {

// TODO Auto-generatedcatch block

e.printStackTrace();

}

}



public static void store(Activity activity,

com.tencent.weibo.beans.OAuth oauth) {

SharedPreferences settings = activity.getSharedPreferences(fileName,

Context.MODE_PRIVATE);



SharedPreferences.Editor editor = settings.edit();



editor.putString("oauth_token", oauth.getOauth_token());

editor.putString("oauth_token_secret", oauth.getOauth_token_secret());



editor.commit();

}



public static String[] fetch(Activity activity) {

SharedPreferences settings = activity.getSharedPreferences(fileName,

Context.MODE_PRIVATE);



String oauth_token = settings.getString("oauth_token", null);

String oauth_token_secret = settings.getString("oauth_token_secret",

null);



return new String[] { oauth_token, oauth_token_secret };

}



public static void clear(Activity activity) {

SharedPreferences settings = activity.getSharedPreferences(fileName,

Context.MODE_PRIVATE);



SharedPreferences.Editor editor = settings.edit();



editor.clear();

editor.commit();

}

}



在AppConst定义了两个静态全局变量

public static String mOauth_verifier = null;



public static String mOauth_token = null;



定义了腾讯微博分享后跳转的Activity TencentWeiboJumpActivity

public class TencentWeiboJumpActivity extends Activity {



@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generatedmethod stub

super.onCreate(savedInstanceState);

Intent intent = new Intent(this, TestActivity.class);

Uri uri = this.getIntent().getData();

if (uri != null) {

AppConst.mOauth_verifier = uri.getQueryParameter("oauth_verifier");

AppConst.mOauth_token = uri.getQueryParameter("oauth_token");

this.startActivity(intent);

}

this.finish();

}



}



在TestActivity的OnResume方法中加入了以下代码

if (AppConst.mOauth_verifier != null && AppConst.mOauth_token != null) {

TencentWeiboUtil.getInstance()

.getToken(AppConst.mOauth_verifier, AppConst.mOauth_token);

AppConst.mOauth_verifier = null;

AppConst.mOauth_token = null;

}



在Mainfest中加入以下信息

<activity

android:name=".TencentWeiboJumpActivity"

android:launchMode="singleTop">

<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:host="OAuthActivity"

android:scheme="qweibo4android"/>

</intent-filter>

</activity>



腾讯微博需要加入的权限

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

更多相关文章

  1. 三种自定义漂亮的Android(安卓)SeekBar的方法
  2. android Fragment 与 Fragment 之间传参(对象)
  3. ListView添加事件并获取选中项的值
  4. android 自定义下拉刷新上拉加载控件(SwipeRefreshLayout + recyc
  5. Volley框架解读(二)
  6. Android(安卓)Camera 调用流程
  7. 【Android】Toast大全
  8. Android(安卓)OpenCV 遇到的错误:Unresolved inclusion
  9. Android——Activity之间传递 实体类(Bean)

随机推荐

  1. AndroidDevTools收集整理Android开发所需
  2. Android布局之xml设置
  3. Android中application的theme不生效的bug
  4. android android:gravity 不起作用
  5. Android 动态加载(五) - 借尸还魂之代理Act
  6. 第一天Android校内实习笔记--回顾
  7. android test frame
  8. Android中Density和Pixel的关系对界面显
  9. Unity Android SDK 接入(一) - Unity 与
  10. 党系列之五:Android UI相关知识总结 Dear_