当前位置:操作系统 > 安卓/Android >>

android新浪分享实例

新浪分享比较简单,新浪有提供完整的demo。


android实现新浪的分享功能,分3种分享情况:

纯文本的,带图片的,图片为本地图片(传入的是图片在手机的地址),第2种带图片的是,网络图片,图片地址为网络地址url

 


首先到新浪开放平台,注册账号,创建应用,注意的是请点击应用,选择高级信息,在里面设置上回调页面

 

 


回调页随便填写一个就行

 


下载sdk包,把包加入libs下面,引入

 


主类:


package com.sharedemo;


import java.io.IOException;
import java.text.SimpleDateFormat;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.net.RequestListener;


public class MainActivity extends Activity {
private Weibo mWeibo;
private static final String CONSUMER_KEY = "1587758551";// App_key 创建应用获得
private static final String REDIRECT_URL = "http://www.sina.com";      //回调地址,必须和注册时候填写的一致
public static Oauth2AccessToken accessToken;
private ProgressDialog dialog;
private Message message;
private UsersAPI usersAPI;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
accessToken = SaveDate.getDate(this);


}


public void sendContent1(View view) { // 发送纯文本的方法
dialog = ProgressDialog.show(this, "处理中", "请稍等");
dialog.setCanceledOnTouchOutside(true);
sendContent();


}


public void getContent(View view) {     //获取用户信息
if (accessToken.isSessionValid()) {
message = new Message();
dialog = ProgressDialog.show(this, "处理中", "请稍等");
usersAPI = new UsersAPI(accessToken);
usersAPI.show(Long.parseLong(SaveDate.get(this, "uid")),
new RequestListener() {


@Override
public void onIOException(IOException arg0) {
Log.i("hck", "onIOException " + arg0);
message.what = 0;
handler.sendMessage(message);
}


@Override
public void onError(WeiboException arg0) {
Log.i("hck", "onError " + arg0);
message.what = 0;
handler.sendMessage(message);
}


@Override
public void onComplete(String arg0) {


message.what = 1;
message.obj = arg0;
message.arg1 = 1;
handler.sendMessage(message);
}
});
} else {
mWeibo.authorize(MainActivity.this, new AuthDialogListener());
}
}


Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (dialog != null) {
dialog.dismiss();
}
if (msg.what == 1 && msg.arg1 != 1) {
Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG)
.show();
} else if (msg.arg1 == 1 && msg.what == 1) {
Toast.makeText(MainActivity.this, msg.obj.toString(),
Toast.LENGTH_LONG).show();
Log.i("hck", msg.obj.toString());
} else {
Toast.makeText(MainActivity.this, "发送失败:" + msg.obj.toString(),
Toast.LENGTH_LONG).show();
}
};
};


public void sendContent2(View view) { // 带图片的方法,图片为本机里面的图片
if (accessToken.isSessionValid()) {
dialog = ProgressDialog.show(this, "处理中", "请稍等");
String fiString = Environment.getExternalStorageDirectory()
.toString() + "/sina.png";
new StatusesAPI(accessToken).upload("图片分享", fiString, "", "",
new listener());
} else {
mWeibo.authorize(MainActivity.this, new AuthDialogListener());
}
}


public void sendContent3(View view) { // 带图片的方法,图片为网络图片
if (accessToken.isSessionValid()) {
dialog = ProgressDialog.show(this, "处理中", "请稍等");
new StatusesAPI(accessToken)
.uploadUrlText(
"图片分享222",
"http://h.hiphotos.baidu.com/album/w%3D2048/sign=ba4fd5d5d0c8a786be2a4d0e5331c83d/d1160924ab18972b4b5775e5e7cd7b899e510aab.jpg",
"", "", new listener());
} else {
mWeibo.authorize(this, new AuthDialogListener());
}
}


private void sendContent() {
if (accessToken.isSessionValid()) {
new StatusesAPI(accessToken).update("新浪微博测试33377777", null, null,
new listener());
} else {
mWeibo.authorize(this, new AuthDialogListener());


}
}


class listener implements RequestListener {


@Override
public void onComplete(String arg0) {
dialog.dismiss();
message = new Message();
message.what = 1;
handler.sendMessage(message);


}


@Override
public void onError(WeiboException arg0) {


dialog.dismiss();
message = new Message();
message.what = 0;
message.obj = arg0;
handler.sendMessage(message);
Log.i("hck", "onError" + arg0 + "");


}


@Override
public void onIOException(IOException arg0) {


dialog.dismiss();
message = new Message();
message.what = 0;
message.obj = arg0;
handler.sendMessage(message);
Log.i("hck", arg0 + "");


}


}


class AuthDialogListener implements WeiboAuthListener {


@Override
public void onComplete(Bundle values) {
String token = values.getString("access_token");
String expires_in = values.getString("expires_in");
String userIdString = values.getString("uid");
accessToken = new Oauth2AccessToken(token, expires_in);
if (accessToken.isSessionValid()) {
SaveDate.save(MainActivity.this, "uid", userIdString);
sendContent();
SaveDate.saveDate(MainActivity.this, accessToken);
}


}


@Override
public void onError(WeiboDialogError e) {
Toast.makeText(getApplicationContext(),
"Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}


@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Auth cancel",
Toast.LENGTH_LONG).show();
}


@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(getApplicationContext(),
"Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
.show();
}


}


}

 

保存数据的类:


package com.sharedemo;


import com.weibo.sdk.android.Oauth2AccessToken;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

<
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,