新浪微博分享功能
注册帐号后,创建应用后,会得到一个唯一的key和secret,如下图
我在工程中使用的是Oauth2.0隐式授权方式,所以需要在新浪微博开发者平台重定义地址,如下图
需要在工程中加入新浪微博demo中com.weibo.net里面的内容,如下图
需要修改Weibo.java里面的代码
private static String APP_KEY = "";
private static String APP_SECRET = "";
将里面的内容替换成你创建项目时候生成的key和secret
public staticfinal String DEFAULT_REDIRECT_URI = "";
这里也要重新设置成你之前重定义的地址
具体可参考新浪微博的例子
自己封装了一个调用新浪微博的类 SinaWeiboUtil
public class SinaWeiboUtil {
private SinaWeiboRequestListener mWeiboRequestListener;
private TestActivity mActivity;
private static SinaWeiboUtil instance;
public static SinaWeiboUtil getInstance() {
return instance;
}
public static void createInstance(TestActivity activity) {
instance = new SinaWeiboUtil(activity);
}
private SinaWeiboUtil(TestActivity activity) {
mActivity = activity;
}
public void sendSinaWeibo() {
Weibo weibo = Weibo.getInstance();
if (AppConst.WACCESSTOKEN != null) {
weibo.setRedirectUrl(Weibo.DEFAULT_REDIRECT_URI);// 此处回调页内容应该替换为与appkey对应的应用回调页
AccessToken accessToken = new AccessToken(AppConst.WACCESSTOKEN,
Weibo.getAppSecret());
weibo.setAccessToken(accessToken);
accessToken.setExpiresIn("86400");
try {
Log.v("tag==========", "secret =" + Weibo.getAppSecret()
+ " token =" + Weibo.getAppKey());
update(weibo, Weibo.getAppKey(), mActivity
.getResources().getString(R.string.smscontent), "", "");
} catch (MalformedURLException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (NotFoundException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (WeiboException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
} else {
// Oauth2.0
// 隐式授权认证方式
weibo.setRedirectUrl(Weibo.DEFAULT_REDIRECT_URI);// 此处回调页内容应该替换为与appkey对应的应用回调页
// 对应的应用回调页可在开发者登陆新浪微博开发平台之后,
// 进入我的应用--应用详情--应用信息--高级信息--授权设置--应用回调页进行设置和查看,
// 应用回调页不可为空
weibo.authorize(mActivity,
new SinaAuthDialogListener(mActivity));
}
}
public void update(Weibo weibo, String source, String status,
String lon, String lat) throws MalformedURLException,IOException,
WeiboException {
WeiboParameters bundle = new WeiboParameters();
bundle.add("source", source);
bundle.add("status", status);
if (!TextUtils.isEmpty(lon)) {
bundle.add("lon", lon);
}
if (!TextUtils.isEmpty(lat)) {
bundle.add("lat", lat);
}
String url = Weibo.SERVER + "statuses/update.json";
AsyncWeiboRunner weiboRunner = new AsyncWeiboRunner(weibo);
if (mWeiboRequestListener == null) {
mWeiboRequestListener = new SinaWeiboRequestListener(mActivity);
}
weiboRunner.request(mActivity, url, bundle, Utility.HTTPMETHOD_POST,
mWeiboRequestListener);
}
}
新浪微博认证对话框接口
SinaAuthDialogListener
public class SinaAuthDialogListenerimplements WeiboDialogListener {
TestActivity mActivity;
public SinaAuthDialogListener(TestActivity activity) {
mActivity = activity;
}
@Override
public void onComplete(Bundle values) {
String token = values.getString("access_token");
String expires_in = values.getString("expires_in");
mActivity.setWAccessToken(token);
Weibo.getInstance();
AccessToken accessToken = new AccessToken(token,
Weibo.getAppSecret());
accessToken.setExpiresIn(expires_in);
Weibo.getInstance().setAccessToken(accessToken);
try {
SinaWeiboUtil.getInstance().update(Weibo.getInstance(),Weibo.getAppKey(), mActivity
.getResources().getString(R.string.smscontent), "", "");
} catch (MalformedURLException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (NotFoundException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (WeiboException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
@Override
public void onError(DialogError e) {
Toast.makeText(mActivity, "Autherror : " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(mActivity, "Authcancel", Toast.LENGTH_LONG).show();
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(mActivity, "Authexception : " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
新浪微博请求接口 SinaWeiboRequestListener
public class SinaWeiboRequestListenerimplements RequestListener {
private TestActivity mActivity;
public SinaWeiboRequestListener(TestActivity activity) {
mActivity = activity;
}
@Override
public void onComplete(String response) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity, R.string.send_sucess,
Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onIOException(IOException e) {
// TODO Auto-generatedmethod stub
}
@Override
public void onError(final WeiboException e) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(
mActivity,
String.format(
mActivity.getString(R.string.send_failed)
+ ":%s", e.getMessage()),
Toast.LENGTH_LONG).show();
}
});
}
}
需要在MainFest中加入以下权限
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">