Android 平台上的Twitter应用

傅涵忍
2023-12-01

原文:http://www.cnblogs.com/and_he/archive/2011/05/23/2054494.html

转载留在以后参考~~


关于Twitter这一块,自发这篇博文之后有很多人问我,有的验证成功了不跳转,或者其它原因什么的,我看了一下,这篇博文里面有写呀,下面以红色粗体文字注明一下

刚进公司,叫我先学习Twitter和Facebook,就类似于国内的微博,或者分享功能,点击某个按钮,出来一个提示框,可以分享到某些地方,这里实现的就是分享到Twitter,当然得要使用代理,因为这是给老外做的,所以得符合他们的习惯

先说一下实现的功能吧,首先运行的时候,会检查是否登陆twitter(通过SharedPreference文件保存登陆状态),如果没有登陆的话会跳转到twitter的登陆认证页面,提示用户输入用户名和密码,这些就是OAuth认证的步骤,不懂的看这儿http://www.cnblogs.com/and_he/archive/2011/05/22/2053477.html

做过新浪微博的都应该知道,首先要申请成为开发者,创建自己的应用,然后会给你一些比如Consumer key和Consumer secret之类的东西,这些东西要先记下来,代码里面要用到的,其余的不啰嗦了,只是要注意一点,创建Twitter应用的时候造成不能忘了填那个Callback URL,我当初就没填,结果整了几天都整不出来结果,也算是我的一点经验吧,下面贴代码,以便于以后查阅

首先我们要用到Twitter封闭好的一些包,这里用到两个twitter4j-core-android和twitter4j-media-support-android,百度一下就出来了

以下是工程目录结构


package com.twitter;

public class CONST {
public static final String CONSUMER_KEY = " IFkj4coaZ9F9Ngfoe1LFuQ " ;
public static final String CONSUMER_SECRET = " HN2NfE4mtL7OqJaUirWfRnK9XKhyWG2vZmAj6AFwfJ8 " ;
public static final String REQUEST_TOKEN_URL = " https://api.twitter.com/oauth/request_token " ;
public static final String ACCESS_TOKEN_URL = " https://api.twitter.com/oauth/access_token " ;
public static final String CALLBACK_URL = " https://api.twitter.com/oauth/authorize " ;
public static final String TWITPIC_API_KEY = " a77709ebb6c51de0e77f723751a5f9a4 " ;
}

下面是Twitter类(主类)

package com.twitter;

import java.io.IOException;
import java.io.InputStream;

import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Twitter extends Activity {
EditText et_content;
// 评论内容
EditText et_des; // 图片描述
TextView tv_num; // 剩余字数
Button btn_send; // 发送按钮

int num = 140 ; // 定义总字数

ProgressDialog progressDialog;
// 当点击发送的时候显示此进度条

SharedPreferences spf;
TwitterConnect tc;
// 定义一个twitter连接对象
boolean connectionStatus = false ; // 定义当前的连接状态为false

String content;
String describle;

@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.twitter);
tc
= new TwitterConnect( this );
spf
= getSharedPreferences( " bravesoft " , Context.MODE_PRIVATE);
et_content
= (EditText) findViewById(R.id.et_content);
et_des
= (EditText) findViewById(R.id.et_des);
tv_num
= (TextView) findViewById(R.id.tv_num);
tv_num.setText(
" 140 " );

progressDialog
= new ProgressDialog( this );
progressDialog.setTitle(
" 正在发送... " );
progressDialog.setMessage(
" 正在发送,请稍候... " );

et_content.addTextChangedListener(
new TextWatcher() {
private CharSequence temp;
private int selectionStart;
private int selectionEnd;

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
temp
= s; // 显示当前输入框中的所有内容
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

@Override
public void afterTextChanged(Editable s) {
int number = num - s.length();
tv_num.setText(
"" + number);
selectionStart
= et_content.getSelectionStart();
selectionEnd
= et_content.getSelectionEnd();
if (temp.length() > num) {
s.delete(selectionStart
- 1 , selectionEnd);
int tempSelection = selectionStart;
et_content.setText(s);
et_content.setSelection(tempSelection);
// 设置光标在最后
}
}
});
btn_send
= (Button) findViewById(R.id.btn_send);
connectionStatus
= spf.getBoolean( " connection_tatus " , false );
btn_send.setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
content
= et_content.getText().toString(); // 得到评论内容
describle = et_des.getText().toString(); // 得到图片描述内容
if (connectionStatus) { // 如果用户处于登陆状态,获取用户头像跟name
progressDialog.show();
GetUserInfoTask getUserInfoTask
= new GetUserInfoTask();
getUserInfoTask.execute(
"" );
}
}
});

if ( ! connectionStatus) {
btn_send.setEnabled(
false );
sendMessage();
}

}

/**
* 如果用户处于登陆状态,获取用户头像跟name
*
*
@author Administrator
*
*/
class GetUserInfoTask extends AsyncTask < String, Integer, String > {

@Override
protected String doInBackground(String... params) {
String result
= "" ;
ConfigurationBuilder confBuild
= new ConfigurationBuilder();
confBuild.setOAuthConsumerKey(CONST.CONSUMER_KEY);
confBuild.setOAuthConsumerSecret(CONST.CONSUMER_SECRET);
confBuild.setOAuthAccessToken(spf.getString(
" oauth_token " , "" ));
confBuild.setOAuthAccessTokenSecret(spf.getString(
" oauth_token_secret " , "" ));
twitter4j.conf.Configuration config
= confBuild.build();
if (TwitterConnect.twitter != null )
TwitterConnect.twitter.shutdown();
TwitterConnect.twitter
= new TwitterFactory(config).getInstance();
InputStream input
= null ;
try {
TwitterConnect.accessToken
= TwitterConnect.twitter
.getOAuthAccessToken();
boolean bTest = false ;
AssetManager am
= getAssets();
try {
input
= am.open( " btn_dokusya_toukou.png " );
}
catch (IOException e) {
e.printStackTrace();
}

if (tc == null ) {
tc
= new TwitterConnect(Twitter. this );
}
bTest
= tc.TwitterContribute(content, input, describle);

if (bTest) {
result
= " ok " ;
}
else {
result
= " failure " ;
}
}
catch (Exception e) {

}
return result;
}

@Override
protected void onPostExecute(String result) {
super .onPostExecute(result);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (result.equals( " ok " )) {
Toast.makeText(Twitter.
this , " 发送成功 " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(Twitter.
this , " 发送失败 " , Toast.LENGTH_LONG).show();
}
}

}

public void sendMessage() {
new Send().start();
}

class Send extends Thread {
@Override
public void run() {
super .run();
try {
Thread.sleep(
3000 );
}
catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent
= new Intent(Twitter. this , TwitterLoginBrowser. class );
startActivityForResult(intent,
1 );
}
}

@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
super .onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 ) {
if (resultCode == 1 ) { // 表示从TwitterLoginBrowser返回
connectionStatus = spf.getBoolean( " connection_tatus " , false );
if (connectionStatus) {
btn_send.setEnabled(
true );
}
}
}
}
}

TwitterConnect类

package com.twitter;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.media.ImageUpload;
import twitter4j.media.ImageUploadFactory;
import twitter4j.media.MediaProvider;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class TwitterConnect {
Context context;
public static Twitter twitter;
public static RequestToken requestToken;
public static AccessToken accessToken;

public TwitterConnect(Context context) {
this .context = context;
twitter
= new TwitterFactory().getInstance();
}

public String twitterLoginOAuth() {
twitter.setOAuthConsumer(CONST.CONSUMER_KEY, CONST.CONSUMER_SECRET);
try {
requestToken
= twitter.getOAuthRequestToken();
return requestToken.getAuthenticationURL() + " &force_login=true " ;
}
catch (TwitterException e) {
e.printStackTrace();
return " http://www.baidu.com " ;
}
// 已经得到临时访问令牌
// Request token=7HMpOfNr5ev1kjcW036mDI1hpvycbb1sRkKK3r6Ax30
// Request token secret=FAhiBFX04lbQhme392htH1LgL8hQxm2p0IJ5Kzlofk
// url= http://api.twitter.com/oauth/authenticate?oauth_token=svrKsiu1ArJ7h24RvHPHeNnzfXqLIebRY7uefydmB9k
}

// 得到用户名
public String getUserName() {
try {
return twitter.showUser(accessToken.getUserId()).getName();
}
catch (Exception e) {
return "" ;
}
}

// 得到用户头像
public Bitmap getUserImage(String userImageUrl) {
Bitmap image
= null ;
try {
URL url
= new URL(userImageUrl);
image
= BitmapFactory.decodeStream(url.openStream());
}
catch (Exception e) {
e.printStackTrace();
}
return image;
}

/**
*
*
@param text
* 评论内容
*
@param input
* 图片的一个InputStream
*
@param description
* 图片描述
*
@return
*/
public boolean TwitterContribute(String text, InputStream input,
String description) {
boolean isOk = false ;
Configuration config
= null ;
ImageUpload uploads
= null ;
try {
Status status
= null ;
if (input != null ) {
if (config == null ) {
ConfigurationBuilder confBuild
= new ConfigurationBuilder();
confBuild.setOAuthConsumerKey(CONST.CONSUMER_KEY);
confBuild.setOAuthConsumerSecret(CONST.CONSUMER_SECRET);
confBuild.setOAuthAccessToken(accessToken.getToken());
confBuild.setOAuthAccessTokenSecret(accessToken
.getTokenSecret());
confBuild.setMediaProviderAPIKey(CONST.TWITPIC_API_KEY);
// 发送图片和对图片的描述
config = confBuild.build();
uploads
= new ImageUploadFactory(config)
.getInstance(MediaProvider.TWITPIC);
}
String uploadre
= "" ;
uploadre
= uploads.upload( " " , input, description);
System.out.println(
" uploadre " + uploadre);
if (input != null )
try {
input.close();
}
catch (IOException e) {
}
if ( ! uploadre.equals( "" )) {
status
= twitter.updateStatus(text + " " + uploadre);
System.out.println(
" uploadre= " + uploadre);
}
}
else {
status
= twitter.updateStatus(text);
}
if (status != null ) {
System.out.println(
" 发表内容: " + status.getText());
isOk
= true ;
}
}
catch (Exception e) {
isOk
= false ;
}
return isOk;
}
}

TwitterLoginBrowser类

package com.twitter;

import twitter4j.TwitterException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class TwitterLoginBrowser extends Activity {
WebView webview;
ProgressDialog progressDialog;
// 定义一个进度条
TwitterConnect tc;
String authenticateUrl
= "" ;

ConnectTask task;

String oauthToken;
String oauthVerifier;

Bitmap userImage;
// 用户头像

@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.twitter_loginbrowser);
progressDialog
= new ProgressDialog(TwitterLoginBrowser. this );
progressDialog.setTitle(
" 请等待 " );
progressDialog.setMessage(
" 正在加载页面,请稍等... " );
progressDialog.show();
// 启动的时候就让它显示
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(
true );
webview.requestFocus();

tc
= new TwitterConnect( this );
task
= new ConnectTask( this );
task.execute(
" login " ); // 执行载入页面任务

webview.setWebViewClient(
new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true ;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super .onPageStarted(view, url, favicon);
System.out.println(
" 开始加载页面: " + url);

}

// 页面载入完成
@Override
public void onPageFinished(WebView view, String url) {
super .onPageFinished(view, url);
// url= https://api.twitter.com/oauth/authorize?oauth_token=9kpqNY7VhmyWC5NRXF2eQ73zN4VKXjQcgZj62sIZU &oauth_verifier=3B7FBAs9XOYH8I3QEQrFGuqJlgMUfQH5fzmz7j3Ws

Toast.makeText(TwitterLoginBrowser.
this , " 页面加载完成 " ,
Toast.LENGTH_LONG).show();
if (url != null && url.startsWith(CONST.CALLBACK_URL)) {
String[] param
= url.split( " \\? " )[ 1 ].split( " & " );
if (param[ 0 ].startsWith( " oauth_token " )) {
oauthToken
= param[ 0 ].split( " = " )[ 1 ];
}
else if (param[ 1 ].startsWith( " oauth_token " )) {
oauthToken
= param[ 1 ].split( " = " )[ 1 ];
}
if (param[ 0 ].startsWith( " oauth_verifier " )) {
oauthVerifier
= param[ 0 ].split( " = " )[ 1 ];
}
else if (param[ 1 ].startsWith( " oauth_verifier " )) {
oauthVerifier
= param[ 1 ].split( " = " )[ 1 ];
}
System.out.println(
" oauthToken= " + oauthToken);
System.out.println(
" oauthVerifier= " + oauthVerifier);
try {
TwitterConnect.accessToken
= TwitterConnect.twitter
.getOAuthAccessToken(
TwitterConnect.requestToken,
oauthVerifier);
WebTask wt
= new WebTask();
wt.execute(
"" );
}
catch (TwitterException e) {
e.printStackTrace();
System.out.println(
" 发生错误了 " );
}
}
}
});
webview.setWebChromeClient(
new WebChromeClient() {
// 控制进度条的显示
@Override
public void onProgressChanged(WebView view, int newProgress) {
super .onProgressChanged(view, newProgress);
if (newProgress != 100 ) {
if ( ! progressDialog.isShowing()) {
progressDialog.show();
}
}
else {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
});
}

// 获取用户名和头像
class WebTask extends AsyncTask < String, Integer, String > {

@Override
protected String doInBackground(String... params) {
String result
= "" ;
if (oauthToken != null && oauthVerifier != null ) {
System.out.println(
" 开始获取用户名和头像 " );
String userImageUrl
= "" ; // 用户头像url
try {
userImageUrl
= TwitterConnect.twitter
.showUser(TwitterConnect.accessToken.getUserId())
.getProfileImageURL().toString();
}
catch (TwitterException e) {
e.printStackTrace();
}
String userName
= tc.getUserName();
if (userImage != null ) {
userImage.recycle();
}
userImage
= tc.getUserImage(userImageUrl);
// 登陆成功 回到来的画面
Intent intent = new Intent();
Bundle bundle
= new Bundle();
bundle.putParcelable(
" userImage " , userImage);
bundle.putString(
" userName " , userName);
intent.putExtra(
" userInfo " , bundle);
// 保存登陆状态
SharedPreferences spf = getSharedPreferences( " bravesoft " ,
Context.MODE_PRIVATE);
Editor editor
= spf.edit();
editor.putString(
" oauth_token " ,
TwitterConnect.accessToken.getToken());
editor.putString(
" oauth_token_secret " ,
TwitterConnect.accessToken.getTokenSecret());
editor.putBoolean(
" connection_tatus " , true );
editor.putString(
" username " , userName);
editor.putString(
" userimgurl " , userImageUrl);
editor.commit();
TwitterLoginBrowser.
this .setResult( 1 , intent);
bundle.clear();
result
= " ok " ;
}
return result;
}

@Override
protected void onPostExecute(String result) {
super .onPostExecute(result);
if (result.equals( " ok " )) {
Toast.makeText(TwitterLoginBrowser.
this , " 登陆成功 " ,
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(TwitterLoginBrowser.
this , " 登陆失败 " ,
Toast.LENGTH_LONG).show();
}
// TwitterLoginBrowser.this.finish();
}

}

class ConnectTask extends AsyncTask < String, Integer, String > {

public ConnectTask(Context context) {

}

@Override
protected String doInBackground(String... params) {
authenticateUrl
= tc.twitterLoginOAuth();
return authenticateUrl;
}

@Override
protected void onPostExecute(String result) {
super .onPostExecute(result);
if ( ! result.equals( "" )) {
webview.loadUrl(result);
// 载入网页
} else {
Toast.makeText(TwitterLoginBrowser.
this , " 载入错误,请重试 " ,
Toast.LENGTH_LONG).show();
TwitterLoginBrowser.
this .finish();
}
}

}

@Override
public boolean onKeyDown( int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
webview.goBack();
return true ;
}
return super .onKeyDown(keyCode, event);
}
}

 类似资料: