android分享到腾讯微博

来源:互联网 发布:网页编程 编辑:程序博客网 时间:2024/04/28 19:30

到腾讯微博注册账号,创建应用。注意点击应用基本信息,填写应用网址,开发时候要用。

下载sdk,解压,把Android_sdk_v1.2.jar加入项目libs里面引入


主类:

package com.sharetenxun;


import java.net.URLEncoder;


import com.tencent.weibo.api.TAPI;
import com.tencent.weibo.api.UserAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;


public class MainActivity extends Activity {
private String backUrl = "http://www.hck.com"; // 回调地址,创建应用的时候自己设置的
private String app_key = "801306361"; // 获取的appkey
private String clientSecret = "494f7fad75a66898047a165fbe69f1c3"; // 注册应用获取
private OAuthV2 oAuth; // 授权用
private UserAPI userAPI; // 用户模块处理对象
private String response; // 返回信息
private Message message;
private TAPI tapi; // 发送微博处理对象


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}


public void login(View view) {
login();
}


Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
Log.i("hck", "response" + msg.obj.toString());
Toast.makeText(MainActivity.this, msg.obj.toString(),
Toast.LENGTH_LONG).show();
} else if (msg.what == 2) {
Log.i("hck", "response" + msg.obj.toString());
Toast.makeText(MainActivity.this, "文本发送成功", Toast.LENGTH_LONG)
.show();
} else if (msg.what == 3) {
Log.i("hck", "response" + msg.obj.toString());
Toast.makeText(MainActivity.this, "带图片的文本发送成功",
Toast.LENGTH_LONG).show();
} else if (msg.what == 0) {
Log.i("hck", "response" + msg.obj.toString());
Toast.makeText(MainActivity.this, "发送成功失败", Toast.LENGTH_LONG)
.show();
}
};
};


public void show1(View view) {
message = new Message();
if (isExit()) {
new Thread() {
@Override
public void run() {
userAPI = new UserAPI(OAuthConstants.OAUTH_VERSION_2_A); // 用oauth2.0创建


try {
response = userAPI.info(oAuth, "json"); // 获取用户信息
message.obj = response;
} catch (Exception e) {
e.printStackTrace();
}
userAPI.shutdownConnection();
super.run();
message.what = 1;
handler.sendMessage(message);
}
}.start();


} else {
login();
}
}


public void send1(View view) {
if (isExit()) {
message = new Message();
tapi = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
try {
                oAuth.setClientId(app_key);
                oAuth.setClientSecret(clientSecret);
response = tapi.add(oAuth, "json", "Android客户端文字微博2",
"127.0.0.1"); // 发送纯文本信息
message.what = 2;
message.obj = response;
} catch (Exception e) {
e.printStackTrace();
Log.e("hck", "Exception  " + e.toString());
message.what = 0;
}
tapi.shutdownConnection();
handler.sendMessage(message);
}
}


public void send2(View view) {
String imageUrl = "http://e.hiphotos.baidu.com/album/w%3D2048/sign=86a790652f738bd4c421b53195b386d6/3c6d55fbb2fb4316313195c221a4462309f7d358.jpg"; // 网络图片地址,你也可以用本地图片
if (isExit()) {
message = new Message();
tapi = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
try {


response = tapi.addPic(oAuth, "json", "微博开放平台测试2222", "22.32.1",
imageUrl);

message.what = 3;
message.obj = response;
} catch (Exception e) {
e.printStackTrace();
Log.e("hck", "Exception  " + e.toString());
message.what = 0;
}
tapi.shutdownConnection();
handler.sendMessage(message);
}
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) { // 授权后进行处理
if (requestCode == 2) {
if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
oAuth = (OAuthV2) data.getExtras().getSerializable("oauth"); // 获取返回的oAuth
if (oAuth.getStatus() == 0)
Log.i("hck",
data.getStringExtra("openid")
+ data.getStringExtra("openkey"));
Toast.makeText(getApplicationContext(), "登陆成功",
Toast.LENGTH_SHORT).show();
SaveDate.saveDate(this, oAuth); // 保存到本地文件中
}
}
}


private boolean isExit() {
oAuth = SaveDate.getDate(this); // 从本地获取oAuth
if (oAuth != null && oAuth.getStatus() == 0) {
return true;
}
return false;
}


private void login() { // 授权
oAuth = new OAuthV2(backUrl);
oAuth.setClientId(app_key);
oAuth.setClientSecret(clientSecret);
Intent intent = new Intent();
intent = new Intent(MainActivity.this, OAuthV2AuthorizeWebView.class);// 创建Intent,使用WebView让用户授权
intent.putExtra("oauth", oAuth);
startActivityForResult(intent, 2);
}


}


有注释,比较简单,不介绍了。


保存授权后的数据信息类

package com.sharetenxun;
import com.tencent.weibo.oauthv2.OAuthV2;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SaveDate {
private static SharedPreferences preferences;


public SaveDate(Context context) {

}
public static void saveDate(Context context, OAuthV2 token) {
preferences = context
.getSharedPreferences("tenxun", Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString("token", token.getAccessToken());   //token
editor.putString("expiresTime", token.getExpiresIn()); //过期时间
editor.putString("openid", token.getOpenid());  
editor.putString("opkey", token.getOpenkey());
editor.putString("key", token.getClientId());
editor.putString("url", token.getClientSecret());
editor.commit();

}
public static OAuthV2 getDate(Context context)
{
OAuthV2 oAuthV2=new OAuthV2();
preferences = context
.getSharedPreferences("tenxun", Context.MODE_PRIVATE);

oAuthV2.setAccessToken(preferences.getString("token", ""));
oAuthV2.setExpiresIn(preferences.getString("expiresTime", null));
oAuthV2.setOpenid(preferences.getString("openid", null));
oAuthV2.setOpenkey(preferences.getString("opkey", null));
oAuthV2.setClientId(preferences.getString("key", null));
oAuthV2.setClientSecret(preferences.getString("url", null));
return oAuthV2;
}

}


xml布局:

<LinearLayoutxmlns: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"

    android:gravity="center"

    android:orientation="vertical">


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="login"

        android:text="授权"/>


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="show1"

        android:text="获取用户信息"/>


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="send1"

        android:text="发送纯文本信息"/>


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="send2"

        android:text="发送带图片的信息"/>




    


</LinearLayout>





原创粉丝点击