http://dev.t.qq.com/ 申请appkey和appsecret.
核心代码:
package com.tencent.weibo.sdk.android.demo; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.PopupWindow; import android.widget.ProgressBar; import android.widget.Toast; import com.tencent.weibo.sdk.android.api.WeiboAPI; import com.tencent.weibo.sdk.android.api.util.Util; import com.tencent.weibo.sdk.android.component.Authorize; import com.tencent.weibo.sdk.android.component.GeneralDataShowActivity; import com.tencent.weibo.sdk.android.component.GeneralInterfaceActivity; import com.tencent.weibo.sdk.android.component.ReAddActivity; import com.tencent.weibo.sdk.android.component.sso.AuthHelper; import com.tencent.weibo.sdk.android.component.sso.OnAuthListener; import com.tencent.weibo.sdk.android.component.sso.WeiboToken; import com.tencent.weibo.sdk.android.model.AccountModel; import com.tencent.weibo.sdk.android.model.BaseVO; import com.tencent.weibo.sdk.android.model.ModelResult; import com.tencent.weibo.sdk.android.network.HttpCallback; public class MainPage_Activity extends Activity { private Button authorize =null; private Button add =null; private Button readd =null; private Context context = null; private WeiboAPI weiboAPI;//微博相关API private String accessToken;// 用户访问令牌 private String requestFormat = "json"; private double longitude = 0d; //初始 private double latitude = 0d; private HttpCallback mCallBack;//回调函数 private PopupWindow loadingWindow = null; private ProgressBar progressBar = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); this.setContentView(R.layout.main_layout); context = this.getApplicationContext(); this.init(); //这里是粗放的错误提示,只做示例,实际应用时应该细化且友好 mCallBack = new HttpCallback() { @Override public void onResult(Object object) { ModelResult result = (ModelResult) object; if(loadingWindow!=null && loadingWindow.isShowing()){ loadingWindow.dismiss(); } if(result!=null && result.isSuccess()){ Toast.makeText(MainPage_Activity.this, "分享成功", Toast.LENGTH_SHORT) .show(); }else{ Toast.makeText(MainPage_Activity.this, "分享失败", Toast.LENGTH_SHORT) .show(); } } }; progressBar = new ProgressBar(this); loadingWindow = new PopupWindow(progressBar,100,100); } public void init(){ authorize = (Button)findViewById(R.id.authorize); authorize.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** * 跳转到授权组件 */ long appid = Long.valueOf(Util.getConfig().getProperty("APP_KEY")); String app_secket = Util.getConfig().getProperty("APP_KEY_SEC"); auth(appid, app_secket); } }); add = (Button)findViewById(R.id.add); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { shareInfo(); } }); readd = (Button)findViewById(R.id.readd); readd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //startReq(); /** * 跳转到一键转播组件 * 可以传递一些参数 * 如content为转播内容 * video_url为转播视频URL * pic_url为转播图片URL */ Intent i = new Intent(MainPage_Activity.this,ReAddActivity.class); Bundle bundle = new Bundle(); bundle.putString("content", "Make U happy"); bundle.putString("video_url", "http://www.tudou.com/programs/view/b-4VQLxwoX4/"); bundle.putString("pic_url", "http://t2.qpic.cn/mblogpic/9c7e34358608bb61a696/2000"); i.putExtras(bundle); startActivity(i); } }); Button delAuthorize = (Button)findViewById(R.id.exit); delAuthorize.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Util.clearSharePersistent(context); Toast.makeText(MainPage_Activity.this,"注销成功", Toast.LENGTH_SHORT) .show(); } }); Button comInterface = (Button)findViewById(R.id.commoninterface); comInterface.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(MainPage_Activity.this,GeneralInterfaceActivity.class); startActivity(i); } }); } private void auth(long appid, String app_secket) { final Context context = this.getApplicationContext(); //注册当前应用的appid和appkeysec,并指定一个OnAuthListener //OnAuthListener在授权过程中实施监听 AuthHelper.register(this, appid, app_secket, new OnAuthListener() { //如果当前设备没有安装腾讯微博客户端,走这里 @Override public void onWeiBoNotInstalled() { Toast.makeText(MainPage_Activity.this, "onWeiBoNotInstalled", 1000) .show(); AuthHelper.unregister(MainPage_Activity.this); Intent i = new Intent(MainPage_Activity.this,Authorize.class); startActivity(i); } //如果当前设备没安装指定版本的微博客户端,走这里 @Override public void onWeiboVersionMisMatch() { Toast.makeText(MainPage_Activity.this, "onWeiboVersionMisMatch", 1000).show(); AuthHelper.unregister(MainPage_Activity.this); Intent i = new Intent(MainPage_Activity.this,Authorize.class); startActivity(i); } //如果授权失败,走这里 @Override public void onAuthFail(int result, String err) { Toast.makeText(MainPage_Activity.this, "result : " + result, 1000) .show(); AuthHelper.unregister(MainPage_Activity.this); } //授权成功,走这里 //授权成功后,所有的授权信息是存放在WeiboToken对象里面的,可以根据具体的使用场景,将授权信息存放到自己期望的位置, //在这里,存放到了applicationcontext中 @Override public void onAuthPassed(String name, WeiboToken token) { Toast.makeText(MainPage_Activity.this, "passed", 1000).show(); // Util.saveSharePersistent(context, "ACCESS_TOKEN", token.accessToken); Util.saveSharePersistent(context, "EXPIRES_IN", String.valueOf(token.expiresIn)); Util.saveSharePersistent(context, "OPEN_ID", token.openID); // Util.saveSharePersistent(context, "OPEN_KEY", token.omasKey); Util.saveSharePersistent(context, "REFRESH_TOKEN", ""); // Util.saveSharePersistent(context, "NAME", name); // Util.saveSharePersistent(context, "NICK", name); Util.saveSharePersistent(context, "CLIENT_ID", Util.getConfig().getProperty("APP_KEY")); Util.saveSharePersistent(context, "AUTHORIZETIME", String.valueOf(System.currentTimeMillis() / 1000l)); AuthHelper.unregister(MainPage_Activity.this); } }); AuthHelper.auth(this, ""); } private void shareInfo(){ accessToken = Util.getSharePersistent(getApplicationContext(), "ACCESS_TOKEN"); if (accessToken == null || "".equals(accessToken)) { Toast.makeText(MainPage_Activity.this, "请先授权", Toast.LENGTH_SHORT).show(); this.finish(); return ; } AccountModel account = new AccountModel(accessToken); weiboAPI = new WeiboAPI(account); weiboAPI.addWeibo(context, "http://chenzheng8975.iteye.com/腾讯微博测试!", requestFormat, longitude, latitude, 0, 0, mCallBack, null, BaseVO.TYPE_JSON); } }