这篇文章中,我们使用到了Share SDK,它是为iOS、Android、WP8的APP提供社会化功能的一个组件,目前支持如QQ、微信、新浪微博、腾讯微博、开心网、人人网、豆瓣、网易微博、搜狐微博、facebook、twitter、google+等国内外主流社交平台。
一、实现的效果图
主界面效果图
授权登录页面效果图
点击分享按钮弹出分享分享界面
二、项目结构目录
三、编码前的准备工作
1、获取Libs
Libs包含ShareSDK的类库, 具体包括三个文件夹,分别是ShareSDK的全局依赖库、ShareSDK当前支持的所有平台工具库和ShareSDK可视化UI的一些支持库。“全局依赖库”是集成ShareSDK的基础,ShareSDK的任何平台都依赖于这个库,而“ShareSDK-GUI”提供的是一个测栏控件和一个快捷分享工具,以方便读者更快速地集成ShareSDK。Libs下载地址
2、导入Libs
<1> 将“Libs\Global-Dependences”下的jar包复制到您的libs目录下。
<2> 从“Libs\Platforms”中选择您感兴趣的平台,比方说“新浪微博”、“QQ空间”、“腾讯微博”等,复制相应的压缩包到你项目的libs目录下并解压
<3> 如果你决定使用shareSDK提供的快捷分享工具,还需要复制“Libs\ShareSDK-GUI”中复制“cn.sharesdk.onekeyshare.jar”到你的项目中。
<4> 一般来说,ADT会自动将你添加到libs目录下的jar包添加到“Android Dependencies”中。但是如果你的开发环境不能自动加载ShareSDK的jar包到你的项目中,那么只能手动添加,如下图所示:
四、详细的编码实现
1、ShareSdK使用统一的格式管理你在不同平台上注册的开发者信息。这些信息都存放在项目的“assets/ShareSDKDevInfor.xml ”中,ShareSDKDevInfor.xml:
xml version = "1.0" encoding = "utf-8" ?>
< DevInfor >
< SinaWeibo
SortId = "1"
AppKey = "3201194191"
AppSecret = "0334252914651e8f76bad63337b3b78f"
Id = "1"
RedirectUrl = "http://appgo.cn" />
< TencentWeibo
SortId = "2"
AppKey = "801307650"
AppSecret = "ae36f4ee3946e1cbb98d6965b0b2ff5c"
RedirectUri = "http://sharesdk.cn"
Id = "2" />
< QZone
SortId = "3"
AppId = "100371282"
AppKey = "aed9b0303e3ed1e27bae87c33761161d"
Id = "3"
RedirectUrl = "http://www.shareSDK.cn" />
< Renren
SortId = "4"
AppId = "226427"
ApiKey = "fc5b8aed373c4c27a05b712acba0f8c3"
Id = "4"
SecretKey = "f29df781abdd4f49beca5a2194676ca4" />
DevInfor >
2、配置AndroidManifest.xml,不同的集成度需要在AndroidManifest.xml中添加的内容也不一样。但是首先你需要添加下面的权限列表:
< uses-permission android:name = "android.permission.INTERNET" />
< uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
< uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
< uses-permission android:name = "android.permission.WRITE_APN_SETTINGS" />
< uses-permission android:name = "android.permission.CHANGE_WIFI_STATE" />
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
< uses-permission android:name = "android.permission.READ_PHONE_STATE" />
这些权限将允许你的项目和ShareSDK获取连接网络的权限、获取你的设备网络状态的权限、实现https安全连接的权限、读取手机设备状态的权限和保存必要配置的权限。一般来说,即便不集成ShareSDK,大部分的项目也都会注册申请这些权限。
注意: 大家在加入这个"android.permission.WRITE_APN_SETTINGS"权限的时候,可能有些读者的编译器会报错,博主就遇到了这样的情况,这个是 ADT Lint工具的问题。
解决的办法是: 依照下面的路径“Window —> Preferences —> android—> lint error checking”打开lint的配置页面,然后去掉页面顶部的两个勾选,之后再clean项目就能处理。如下图所示:
3、其次,为了授权操作可以顺利完成,需要在application下注册下面的Activity:
< activity
android:name = "cn.sharesdk.framework.AuthorizeActivity"
android:configChanges = "keyboardHidden|orientation"
android:screenOrientation = "portrait"
android:theme = "@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode = "stateHidden|adjustResize" >
activity >
AuthorizeActivity的路径是固定的,一定要在“cn.sharesdk.framework”下,因为他在Share-Core包中。 4、添加布局页面,首先是主界面的布局页面,activity_main.xml:
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent" >
< Button
android:id = "@+id/btnLogin"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_above = "@+id/btnShareAllGui"
android:layout_centerHorizontal = "true"
android:layout_margin = "5dp"
android:background = "@drawable/btn_back"
android:text = "用户授权登录"
android:textSize = "16dp" />
< Button
android:id = "@+id/btnShareAllGui"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_above = "@+id/btnShareAll"
android:layout_margin = "5dp"
android:background = "@drawable/btn_back"
android:text = "分享全部(有分享界面)"
android:textSize = "16dp" />
< Button
android:id = "@+id/btnShareAll"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_alignParentRight = "true"
android:layout_centerVertical = "true"
android:layout_margin = "5dp"
android:background = "@drawable/btn_back"
android:text = "分享全部(无界面,直接分享)"
android:textSize = "16dp" />
< Button
android:id = "@+id/btnUserInfo"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_below = "@+id/btnShareAll"
android:layout_margin = "5dp"
android:layout_marginTop = "41dp"
android:background = "@drawable/btn_back"
android:text = "获取授权用户资料"
android:textSize = "16dp" />
RelativeLayout >
5、用户授权登录的布局页面,activity_auth.xml:
6、获得用户信息布局界面,activity_userinfo.xml:
xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "#fff5f5f5"
android:orientation = "vertical" >
< cn.sharesdk.framework.TitleLayout
android:id = "@+id/llTitle"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:background = "@drawable/title_back" />
< Button
android:id = "@+id/btnQz"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_centerVertical = "true"
android:layout_margin = "5dp"
android:background = "@drawable/btn_back"
android:text = "@string/get_user_info_qz"
android:textSize = "16dp" />
< Button
android:id = "@+id/btnRr"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_above = "@+id/btnQz"
android:layout_margin = "5dp"
android:background = "@drawable/btn_back"
android:text = "@string/get_user_info_rr"
android:textSize = "16dp" />
< Button
android:id = "@+id/btnSw"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_above = "@+id/btnRr"
android:layout_margin = "5dp"
android:background = "@drawable/btn_back"
android:text = "@string/get_user_info_sw"
android:textSize = "16dp" />
< Button
android:id = "@+id/btnTc"
android:layout_width = "fill_parent"
android:layout_height = "44dp"
android:layout_below = "@+id/btnQz"
android:layout_centerHorizontal = "true"
android:layout_margin = "5dp"
android:background = "@drawable/btn_back"
android:text = "@string/get_user_info_tc"
android:textSize = "16dp" />
RelativeLayout >
7、显示用户获得的信息布局界面,activity_userinfo.xml:
xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "#fff5f5f5"
android:orientation = "vertical" >
< cn.sharesdk.framework.TitleLayout
android:id = "@+id/llTitle"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:background = "@drawable/title_back" />
< ScrollView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_alignParentBottom = "true"
android:layout_below = "@id/llTitle"
android:paddingBottom = "10dp"
android:paddingLeft = "10dp"
android:paddingTop = "10dp" >
< TextView
android:id = "@+id/tvJson"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_marginRight = "10dp"
android:autoLink = "all"
android:background = "@drawable/list_item_single_normal"
android:textColor = "#ff000000" />
ScrollView >
RelativeLayout >
8、主界面入口Activity类,MainActivity.java:
package com.yangyu.activity;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.onekeyshare.ShareAllGird;
import com.yangyu.mysharethings.R;
public class MainActivity extends Activity implements OnClickListener {
public static String TEST_IMAGE;
private Button authLoginBtn,shareGuiBtn,shareBtn,getInfoBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AbstractWeibo.initSDK(this );
initImagePath();
initView();
initData();
}
private void initView(){
authLoginBtn = (Button)findViewById(R.id.btnLogin);
shareGuiBtn = (Button)findViewById(R.id.btnShareAllGui);
shareBtn = (Button)findViewById(R.id.btnShareAll);
getInfoBtn = (Button)findViewById(R.id.btnUserInfo);
}
private void initData(){
authLoginBtn.setOnClickListener(this );
shareGuiBtn.setOnClickListener(this );
shareBtn.setOnClickListener(this );
getInfoBtn.setOnClickListener(this );
}
private void initImagePath() {
try {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& Environment.getExternalStorageDirectory().exists()) {
TEST_IMAGE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/pic.png" ;
}
else {
TEST_IMAGE = getApplication().getFilesDir().getAbsolutePath() + "/pic.png" ;
}
File file = new File(TEST_IMAGE);
if (!file.exists()) {
file.createNewFile();
Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
FileOutputStream fos = new FileOutputStream(file);
pic.compress(CompressFormat.JPEG, 100 , fos);
fos.flush();
fos.close();
}
} catch (Throwable t) {
t.printStackTrace();
TEST_IMAGE = null ;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:
startActivity(new Intent(MainActivity. this ,AuthActivity. class ));
break ;
case R.id.btnShareAllGui:
showGrid(false );
break ;
case R.id.btnShareAll:
showGrid(true );
break ;
case R.id.btnUserInfo:
Intent i = new Intent( this , GetInforActivity. class );
startActivity(i);
break ;
default :
break ;
}
}
private void showGrid( boolean silent) {
Intent i = new Intent( this , ShareAllGird. class );
i.putExtra("notif_icon" , R.drawable.ic_launcher);
i.putExtra("notif_title" , this .getString(R.string.app_name));
i.putExtra("title" , this .getString(R.string.share));
i.putExtra("titleUrl" , "http://sharesdk.cn" );
i.putExtra("text" , this .getString(R.string.share_content));
i.putExtra("imagePath" , MainActivity.TEST_IMAGE);
i.putExtra("url" , "http://sharesdk.cn" );
i.putExtra("thumbPath" , MainActivity.TEST_IMAGE);
i.putExtra("appPath" , MainActivity.TEST_IMAGE);
i.putExtra("comment" , this .getString(R.string.share));
i.putExtra("site" , this .getString(R.string.app_name));
i.putExtra("siteUrl" , "http://sharesdk.cn" );
i.putExtra("silent" , silent);
this .startActivity(i);
}
public static String actionToString( int action) {
switch (action) {
case AbstractWeibo.ACTION_AUTHORIZING: return "ACTION_AUTHORIZING" ;
case AbstractWeibo.ACTION_GETTING_FRIEND_LIST: return "ACTION_GETTING_FRIEND_LIST" ;
case AbstractWeibo.ACTION_FOLLOWING_USER: return "ACTION_FOLLOWING_USER" ;
case AbstractWeibo.ACTION_SENDING_DIRECT_MESSAGE: return "ACTION_SENDING_DIRECT_MESSAGE" ;
case AbstractWeibo.ACTION_TIMELINE: return "ACTION_TIMELINE" ;
case AbstractWeibo.ACTION_USER_INFOR: return "ACTION_USER_INFOR" ;
case AbstractWeibo.ACTION_SHARE: return "ACTION_SHARE" ;
default : {
return "UNKNOWN" ;
}
}
}
protected void onDestroy() {
AbstractWeibo.stopSDK(this );
super .onDestroy();
}
}
集成ShareSDK需要至少在两个地方添加代码,包括:
<1> 在onCreate中插入下面的代码:
AbstractWeibo.initSDK(this );
这行代码会初始化ShareSDK,此后对ShareSDK的操作都依次为基础。如果不在所有ShareSDK的操作之前调用这行代码,会抛出空指针异常。
<2> 在项目的出口Activity的onDestroy方法的第一行插入下面的代码:
protected void onDestroy() {
AbstractWeibo.stopSDK(this );
super .onDestroy();
}
这行代码会结束ShareSDK的统计功能并释放资源。如果这行代码没有被调用,那么
“应用启动次数”的统计将不会准确,因为应用可能从来没有被关闭。
InitSDK是可以重复调用的,其实ShareSDK建议在你不确定的时候调用这个方法,
来保证ShareSDK被正确初始化。而stopSDK一旦调用了,就必须重新调用InitSDK才能使
用ShareSDK的功能,否则会出现空指针异常。
在这段代码中,还使用到了快捷分享 ,如下图所示,点击按钮弹出快捷分享界面:
什么是快捷分享呢?快捷分享是ShareSDK提供的一套基于其接口的GUI。通过简单的配置,可以在不考虑平台的情况下,调用很少的代码,就完成分享的操作。快捷分享的jar包放在SDK解压目录的"Libs\ShareSDK-GUI"中,叫做"cn.sharesdk.oneshare.jar"。 快捷分享使用了两个Activity,需要在AndroidManifest.xml中注册这两个Activity:
< activity
android:name = "cn.sharesdk.onekeyshare.ShareAllGird"
android:configChanges = "keyboardHidden|orientation"
android:screenOrientation = "portrait"
android:theme = "@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode = "adjustPan|stateHidden" />
< activity
android:name = "cn.sharesdk.onekeyshare.SharePage"
android:configChanges = "keyboardHidden|orientation"
android:screenOrientation = "portrait"
android:windowSoftInputMode = "stateHidden|adjustResize" />
9、帐号授权登录界面,AuthActivity.java:
package com.yangyu.activity;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckedTextView;
import android.widget.Toast;
import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.framework.TitleLayout;
import cn.sharesdk.framework.WeiboActionListener;
import cn.sharesdk.renren.Renren;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.tencent.weibo.TencentWeibo;
import com.yangyu.mysharethings.R;
public class AuthActivity extends Activity implements Callback, OnClickListener, WeiboActionListener {
private CheckedTextView sinaCt,qzoneCt,tengxunCt,renrenCt;
private Handler handler;
private TitleLayout llTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
initView();
initData();
}
private void initView(){
handler = new Handler( this );
llTitle = (TitleLayout) findViewById(R.id.llTitle);
sinaCt = (CheckedTextView)findViewById(R.id.ctvSw);
qzoneCt = (CheckedTextView)findViewById(R.id.ctvQz);
tengxunCt = (CheckedTextView)findViewById(R.id.ctvTc);
renrenCt = (CheckedTextView)findViewById(R.id.ctvRr);
}
private void initData(){
llTitle.getBtnBack().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
llTitle.getTvTitle().setText("用户授权登录" );
sinaCt.setOnClickListener(this );
qzoneCt.setOnClickListener(this );
tengxunCt.setOnClickListener(this );
renrenCt.setOnClickListener(this );
AbstractWeibo[] weibos = AbstractWeibo.getWeiboList(this );
for ( int i = 0 ;i < weibos.length;i++){
if (!weibos[i].isValid()) {
continue ;
}
CheckedTextView ctv = getView(weibos[i]);
if (ctv != null ) {
ctv.setChecked(true );
String userName = weibos[i].getDb().get("nickname" );
if (userName == null || userName.length() <= 0 || "null" .equals(userName)) {
userName = getWeiboName(weibos[i]);
weibos[i].setWeiboActionListener(this );
weibos[i].showUser(null );
}
ctv.setText(userName);
}
}
}
private CheckedTextView getView(AbstractWeibo weibo) {
if (weibo == null ) {
return null ;
}
String name = weibo.getName();
if (name == null ) {
return null ;
}
View v = null ;
if (SinaWeibo.NAME.equals(name)) {
v = findViewById(R.id.ctvSw);
}
else if (TencentWeibo.NAME.equals(name)) {
v = findViewById(R.id.ctvTc);
}
else if (Renren.NAME.equals(name)) {
v = findViewById(R.id.ctvRr);
}
else if (QZone.NAME.equals(name)) {
v = findViewById(R.id.ctvQz);
}
if (v == null ) {
return null ;
}
if (! (v instanceof CheckedTextView)) {
return null ;
}
return (CheckedTextView) v;
}
private String getWeiboName(AbstractWeibo weibo) {
if (weibo == null ) {
return null ;
}
String name = weibo.getName();
if (name == null ) {
return null ;
}
int res = 0 ;
if (SinaWeibo.NAME.equals(name)) {
res = R.string.sinaweibo;
}
else if (TencentWeibo.NAME.equals(name)) {
res = R.string.tencentweibo;
}
else if (Renren.NAME.equals(name)) {
res = R.string.renren;
}
else if (QZone.NAME.equals(name)) {
res = R.string.qzone;
}
if (res == 0 ) {
return name;
}
return this .getResources().getString(res);
}
@Override
public void onClick(View v) {
AbstractWeibo weibo = getWeibo(v.getId());
CheckedTextView ctv = (CheckedTextView) v;
if (weibo == null ) {
ctv.setChecked(false );
ctv.setText(R.string.not_yet_authorized);
return ;
}
if (weibo.isValid()) {
weibo.removeAccount();
ctv.setChecked(false );
ctv.setText(R.string.not_yet_authorized);
return ;
}
weibo.setWeiboActionListener(this );
weibo.showUser(null );
}
private AbstractWeibo getWeibo( int vid) {
String name = null ;
switch (vid) {
case R.id.ctvSw:
name = SinaWeibo.NAME;
break ;
case R.id.ctvTc:
name = TencentWeibo.NAME;
break ;
case R.id.ctvRr:
name = Renren.NAME;
break ;
case R.id.ctvQz:
name = QZone.NAME;
break ;
}
if (name != null ) {
return AbstractWeibo.getWeibo( this , name);
}
return null ;
}
@Override
public void onComplete(AbstractWeibo weibo, int action,HashMap res) {
Message msg = new Message();
msg.arg1 = 1 ;
msg.arg2 = action;
msg.obj = weibo;
handler.sendMessage(msg);
}
@Override
public void onError(AbstractWeibo weibo, int action, Throwable t) {
t.printStackTrace();
Message msg = new Message();
msg.arg1 = 2 ;
msg.arg2 = action;
msg.obj = weibo;
handler.sendMessage(msg);
}
@Override
public void onCancel(AbstractWeibo weibo, int action) {
Message msg = new Message();
msg.arg1 = 3 ;
msg.arg2 = action;
msg.obj = weibo;
handler.sendMessage(msg);
}
@Override
public boolean handleMessage(Message msg) {
AbstractWeibo weibo = (AbstractWeibo) msg.obj;
String text = MainActivity.actionToString(msg.arg2);
switch (msg.arg1) {
case 1 : {
text = weibo.getName() + " completed at " + text;
Toast.makeText(this , text, Toast.LENGTH_SHORT).show();
}
break ;
case 2 : {
text = weibo.getName() + " caught error at " + text;
Toast.makeText(this , text, Toast.LENGTH_SHORT).show();
return false ;
}
case 3 : {
text = weibo.getName() + " canceled at " + text;
Toast.makeText(this , text, Toast.LENGTH_SHORT).show();
return false ;
}
}
CheckedTextView ctv = getView(weibo);
if (ctv != null ) {
ctv.setChecked(true );
String userName = weibo.getDb().get("nickname" );
if (userName == null || userName.length() <= 0
|| "null" .equals(userName)) {
userName = getWeiboName(weibo);
}
ctv.setText(userName);
}
return false ;
}
}
10、获取用户信息界面,GetInfoActivity.java:
package com.yangyu.activity;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.framework.TitleLayout;
import cn.sharesdk.framework.WeiboActionListener;
import cn.sharesdk.renren.Renren;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.tencent.weibo.TencentWeibo;
import com.yangyu.mysharethings.R;
public class GetInforActivity extends Activity implements Callback, OnClickListener, WeiboActionListener {
private TitleLayout llTitle;
private Button sinaBt,renrenBt,qzoneBt,tengxunBt;
private Handler handler;
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
handler = new Handler( this );
setContentView(R.layout.activity_userinfo);
initView();
initData();
}
private void initView(){
llTitle = (TitleLayout) findViewById(R.id.llTitle);
sinaBt = (Button) findViewById(R.id.btnSw);
renrenBt = (Button) findViewById(R.id.btnRr);
qzoneBt = (Button) findViewById(R.id.btnQz);
tengxunBt = (Button) findViewById(R.id.btnTc);
}
private void initData(){
llTitle.getBtnBack().setOnClickListener(this );
llTitle.getTvTitle().setText(R.string.get_my_info);
sinaBt.setOnClickListener(this );
renrenBt.setOnClickListener(this );
qzoneBt.setOnClickListener(this );
tengxunBt.setOnClickListener(this );
}
@Override
public void onClick(View v) {
if (v.equals(llTitle.getBtnBack())) {
finish();
return ;
}
String name = null ;
switch (v.getId()) {
case R.id.btnSw:
name = SinaWeibo.NAME;
break ;
case R.id.btnTc:
name = TencentWeibo.NAME;
break ;
case R.id.btnRr:
name = Renren.NAME;
break ;
case R.id.btnQz:
name = QZone.NAME;
break ;
}
if (name != null ) {
AbstractWeibo weibo = AbstractWeibo.getWeibo(this , name);
weibo.setWeiboActionListener(this );
String account = null ;
weibo.showUser(account);
}
}
public void onComplete(AbstractWeibo weibo, int action,HashMap res) {
Message msg = new Message();
msg.arg1 = 1 ;
msg.arg2 = action;
msg.obj = weibo;
handler.sendMessage(msg);
Message msg2 = new Message();
msg2.what = 1 ;
JsonUtils ju = new JsonUtils();
String json = ju.fromHashMap(res);
msg2.obj = ju.format(json);
handler.sendMessage(msg2);
}
public void onError(AbstractWeibo weibo, int action, Throwable t) {
t.printStackTrace();
Message msg = new Message();
msg.arg1 = 2 ;
msg.arg2 = action;
msg.obj = weibo;
handler.sendMessage(msg);
}
public void onCancel(AbstractWeibo weibo, int action) {
Message msg = new Message();
msg.arg1 = 3 ;
msg.arg2 = action;
msg.obj = weibo;
handler.sendMessage(msg);
}
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 1 : {
Intent i = new Intent( this , ShowInforActivity. class );
i.putExtra("data" , String.valueOf(msg.obj));
startActivity(i);
}
break ;
default : {
AbstractWeibo weibo = (AbstractWeibo) msg.obj;
String text = MainActivity.actionToString(msg.arg2);
switch (msg.arg1) {
case 1 : {
text = weibo.getName() + " completed at " + text;
}
break ;
case 2 : {
text = weibo.getName() + " caught error at " + text;
}
break ;
case 3 : {
text = weibo.getName() + " canceled at " + text;
}
break ;
}
Toast.makeText(this , text, Toast.LENGTH_SHORT).show();
}
break ;
}
return false ;
}
}
11、显示用户信息界面,ShowInfoActivity.java:
package com.yangyu.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.sharesdk.framework.TitleLayout;
import com.yangyu.mysharethings.R;
public class ShowInforActivity extends Activity implements OnClickListener {
private TitleLayout llTitle;
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_show_userinfo);
llTitle = (TitleLayout) findViewById(R.id.llTitle);
llTitle.getBtnBack().setOnClickListener(this );
llTitle.getTvTitle().setText("用户资料" );
TextView tvJson = (TextView) findViewById(R.id.tvJson);
tvJson.setText(getIntent().getStringExtra("data" ));
}
@Override
public void onClick(View v) {
if (v.equals(llTitle.getBtnBack())) {
finish();
}
}
}
12、这里还定义了一个Json解析类去读取授权用户的信息,JsonUtils.java:
package com.yangyu.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUtils {
public HashMap fromJson(String jsonStr) {
try {
if (jsonStr.startsWith( "[" )
&& jsonStr.endsWith("]" )) {
jsonStr = "{\"fakelist\":" + jsonStr + "}" ;
}
JSONObject json = new JSONObject(jsonStr);
return fromJson(json);
} catch (Throwable t) {
t.printStackTrace();
}
return new HashMap();
}
private HashMap fromJson(JSONObject json) throws JSONException {
HashMap map = new HashMap();
@SuppressWarnings ( "unchecked" )
Iterator iKey = json.keys();
while (iKey.hasNext()) {
String key = iKey.next();
Object value = json.opt(key);
if (JSONObject.NULL.equals(value)) {
value = null ;
}
if (value != null ) {
if (value instanceof JSONObject) {
value = fromJson((JSONObject)value);
}
else if (value instanceof JSONArray) {
value = fromJson((JSONArray)value);
}
map.put(key, value);
}
}
return map;
}
private ArrayList fromJson(JSONArray array)
throws JSONException {
ArrayList list = new ArrayList();
for ( int i = 0 , size = array.length(); i < size; i++) {
Object value = array.opt(i);
if (value instanceof JSONObject) {
value = fromJson((JSONObject)value);
}
else if (value instanceof JSONArray) {
value = fromJson((JSONArray)value);
}
list.add(value);
}
return list;
}
public String fromHashMap(HashMap map) {
try {
return getJSONObject(map).toString();
} catch (Throwable t) {
t.printStackTrace();
}
return "" ;
}
@SuppressWarnings ( "unchecked" )
private JSONObject getJSONObject(HashMap map)
throws JSONException {
JSONObject json = new JSONObject();
for (Entry entry : map.entrySet()) {
Object value = entry.getValue();
if (value instanceof HashMap, ?>) {
value = getJSONObject((HashMap)value);
}
else if (value instanceof ArrayList>) {
value = getJSONArray((ArrayList)value);
}
json.put(entry.getKey(), value);
}
return json;
}
@SuppressWarnings ( "unchecked" )
private JSONArray getJSONArray(ArrayList list)
throws JSONException {
JSONArray array = new JSONArray();
for (Object value : list) {
if (value instanceof HashMap, ?>) {
value = getJSONObject((HashMap)value);
}
else if (value instanceof ArrayList>) {
value = getJSONArray((ArrayList)value);
}
array.put(value);
}
return array;
}
public String format(String jsonStr) {
try {
return format( "" , fromJson(jsonStr));
} catch (Throwable t) {
t.printStackTrace();
}
return "" ;
}
@SuppressWarnings ( "unchecked" )
private String format(String sepStr, HashMap map) {
StringBuffer sb = new StringBuffer();
sb.append("{\n" );
String mySepStr = sepStr + "\t" ;
int i = 0 ;
for (Entry entry : map.entrySet()) {
if (i > 0 ) {
sb.append(",\n" );
}
sb.append(mySepStr).append('\"' ).append(entry.getKey()).append("\ ":" );
Object value = entry.getValue();
if (value instanceof HashMap, ?>) {
sb.append(format(mySepStr, (HashMap)value));
}
else if (value instanceof ArrayList>) {
sb.append(format(mySepStr, (ArrayList)value));
}
else if (value instanceof String) {
sb.append('\"' ).append(value).append( '\"' );
}
else {
sb.append(value);
}
i++;
}
sb.append('\n' ).append(sepStr).append( '}' );
return sb.toString();
}
@SuppressWarnings ( "unchecked" )
private String format(String sepStr, ArrayList list) {
StringBuffer sb = new StringBuffer();
sb.append("[\n" );
String mySepStr = sepStr + "\t" ;
int i = 0 ;
for (Object value : list) {
if (i > 0 ) {
sb.append(",\n" );
}
sb.append(mySepStr);
if (value instanceof HashMap, ?>) {
sb.append(format(mySepStr, (HashMap)value));
}
else if (value instanceof ArrayList>) {
sb.append(format(mySepStr, (ArrayList)value));
}
else if (value instanceof String) {
sb.append('\"' ).append(value).append( '\"' );
}
else {
sb.append(value);
}
i++;
}
sb.append('\n' ).append(sepStr).append( ']' );
return sb.toString();
}
}
讲到这里关于这一篇的内容就差不多已经讲完了,下一篇是关于微信开放平台的授权以及分享,因为微信平台不同于其它第三方平台,实现起来稍微复杂一点,所以博主打算在下一篇的文章中详细讲解。这个下一篇文章估计要等到三天后了吧,博主和女友早就商量好趁着三天小长假出去散散心了,在这里祝大家端午节快乐!博主将会长期坚持更新关于Android、Java、Cocos2d-x、Unity3d等各个不同知识领域的实例讲解和开发!希望大家继续支持!
源码下载地址