【实例简介】
【实例截图】
【核心代码】
package com.dmmap.demo;
import com.android.utils.TokenStore;
import com.tencent.weibo.api.T_API;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.OAuthClient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TencentMBActivity extends Activity {
private Button btn_share;
private EditText text;
String[] oauth_token_array;
String oauth_token_secret;
String oauth_token;
public static OAuthClient auth;
public static OAuth oauth;
public static final String appName = "QWeibo4Android";
public String picPath = "/mnt/sdcard/android.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_share = (Button) findViewById(R.id.btnshare);
text = (EditText) findViewById(R.id.text);
btn_share.setOnClickListener(new listener());
Button btn_clear = (Button) findViewById(R.id.btnclear);
btn_clear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TokenStore.clear(TencentMBActivity.this);
Toast.makeText(TencentMBActivity.this, "unbind success",
Toast.LENGTH_LONG).show();
}
});
// appkey
oauth = new OAuth("801094267", "17a7d99b68bc2e786313319a3fc25b80",
"null");
// oauth=new
// OAuth("801095243","3dc6632f151dbb2a1c4333e954ae5b5f","null");
}
class listener implements OnClickListener {
public void onClick(View v) {
oauth_token_array = TokenStore.fetch(TencentMBActivity.this);
oauth_token = oauth_token_array[0];
oauth_token_secret = oauth_token_array[1];
if (oauth_token != null && oauth_token_secret != null) { // 宸茬粡鏈塧ccess
// token
oauth.setOauth_token(oauth_token);
oauth.setOauth_token_secret(oauth_token_secret);
T_API tapi = new T_API();
try {
String s = text.getText().toString();
if (!s.equals("")) {
tapi.add(oauth, "json", s, "", "", "");
Toast.makeText(TencentMBActivity.this, "success!",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
Toast.makeText(TencentMBActivity.this, "unbound", Toast.LENGTH_LONG)
.show();
try {
auth = new OAuthClient();// OAuth 璁よ瘉瀵硅薄
// 鑾峰彇request token
oauth = auth.requestToken(oauth);
if (oauth.getStatus() == 1) {
System.out.println("Get Request Token failed!");
return;
} else {
oauth_token = oauth.getOauth_token();
String url = "http://open.t.qq.com/cgi-bin/authorize?oauth_token="
oauth_token;
Log.d(appName, "AndroidExample url = " url);
Intent intent = new Intent(TencentMBActivity.this,
MyWebView.class);
intent.putExtra("URL", url);
startActivity(intent);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
}
public void setToken(String oauth_token, String oauth_token_secret) {
oauth.setOauth_token(oauth_token);
oauth.setOauth_token_secret(oauth_token_secret);
}
}