1.仅用新浪微博的sdk实现分享文字
2.采用的oauth2.0实现的首先将jar包引入
3.项目中的回调地址一定要和在新浪微博开放平台上的回调地址一样
private static final String Sina_CONSUMER_KEY = "新浪微博开放平台上注册的应用的id号";
private static final String Sina_CONSUMER_SECRET = "新浪微博上的";
private static final String Sina_MREDIRECTURL = "回调地址";
private Weibo weibo;
onCreate()里
一个方法oauth()
public voud oauth(){
weibo = Weibo.getInstance();
weibo.setupConsumerConfig(Sina_CONSUMER_KEY, Sina_CONSUMER_SECRET);
// 授权回调页
weibo.setRedirectUrl(Sina_MREDIRECTURL);
Utility.setAuthorization(new Oauth2AccessTokenHeader());
startActivity(WeiboActivity.class);
}
3.再新建一个WeboActivity的类,可以继承Activity 这里面的DecorActivity类是项目中自己写的
4.分享文字
if(weibo!=null){
shareContent();
}else{
oauth();
}
public vodi shareContent(){
String url = Weibo.SERVER + "statuses/update.json";
final WeiboParameters bundle = new WeiboParameters();
bundle.add("source", Weibo.getAppKey());
String content="拍蜜优";
bundle.add("status", content);
String lon = "";
String lat = "";
if (!TextUtils.isEmpty(lon)) {
bundle.add("lon", lon);
}
if (!TextUtils.isEmpty(lat)) {
bundle.add("lat", lat);
}
String filePath ="";
if (filePath != null && filePath.length() > 0) {
bundle.add("pic", filePath);
url = "https://upload.api.weibo.com/2/statuses/upload.json";
}
try {
weibo.request(this, url, bundle, Utility.HTTPMETHOD_POST, weibo.getAccessToken());
Toast.makeText(this, "分享成功", Toast.LENGTH_LONG).show();
} catch (WeiboException e) {
e.printStackTrace();
Toast.makeText(this, "分享失败", Toast.LENGTH_LONG).show();
}
}
package com.newstar.luckybee.blog.sina;
import java.io.IOException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.newstar.luckybee.R;
import com.newstar.luckybee.activity.DecorActivity;
import com.weibo.net.AccessToken;
import com.weibo.net.AsyncWeiboRunner.RequestListener;
import com.weibo.net.DialogError;
import com.weibo.net.Utility;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;
public class WeiboActivity extends DecorActivity implements WeiboDialogListener{
public WebView web_blog;
private final String TAG = "MySinaActivity";
private WebView mWebView;
private WeiboDialogListener mListener;
private LinearLayout linearLayout;
private Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListener = this;
setContentView(R.layout.weibo_activity_layout);
}
@Override
protected void initViews() {
super.initViews();
linearLayout = (LinearLayout) findViewById(R.id.ll_webview);
mWebView = (WebView) findViewById(R.id.mywebview);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WeiboWebViewClient());
mWebView.requestFocus();
mWebView.loadUrl(MySinaManager.getOauthURL(this));
}
private class WeiboWebViewClient extends WebViewClient {
/***
* 地址改变都会调用
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "Redirect URL: " + url);
if (url.startsWith(Weibo.getInstance().getRedirectUrl())) {
handleRedirectUrl(view, url);
return true;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
mListener.onError(new DialogError(description, errorCode,
failingUrl));
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d(TAG, "onPageStarted URL: " + url);
// google issue. shouldOverrideUrlLoading not executed
/**
* 点击授权,url正确
*/
if (url.startsWith(Weibo.getInstance().getRedirectUrl())) {
handleRedirectUrl(view, url);
view.stopLoading();
return;
}
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "onPageFinished URL: " + url);
super.onPageFinished(view, url);
linearLayout.setVisibility(View.GONE);
}
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
handler.proceed();
}
}
private void handleRedirectUrl(WebView view, String url) {
Bundle values = Utility.parseUrl(url);
String error = values.getString("error");
String error_code = values.getString("error_code");
// 授权成功
if (error == null && error_code == null) {
mListener.onComplete(values);
// 拒绝失败等
} else if (error.equals("access_denied")) {
mListener.onCancel();
} else {
// 异常
mListener.onWeiboException(new WeiboException(error, Integer
.parseInt(error_code)));
}
}
public void onComplete(Bundle values) {
/***
* 在这里要save the access_token
*/
String token = values.getString("access_token");
long expires_in = Long.parseLong(values.getString("expires_in"));
Log.i(TAG,"token="+token+"expires_in="+expires_in);
AccessToken access = new AccessToken(token, Weibo.getAppSecret());
Weibo.getInstance().setAccessToken(access);
//这里是授权成功要做的事情
Toast.makeText(WeiboActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
finish();
}
public void onWeiboException(WeiboException e) {
e.printStackTrace();
}
public void onError(DialogError e) {
e.printStackTrace();
}
public void onCancel() {
finish();
}
}