新浪微博分享功能
注冊帳號后,創建應用后,會得到一個唯一的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">
