join the conversation! twitter is your go-to social networking app and the source for what's happening in the world. from world news to local news, entertainment to sports and gaming, politics to fun stories that go viral, when it happens in the world, it happens on twitter first. find friends or follow influential people - every voice can impact the world! join over 2 billion users worldwide!
tweet, retweet, reply to tweets, share or like - twitter is easy to use
chat privately or go big and initiate a group conversation with anyone who follows you. track your friends & other twitter followers or follow your favorite celebrity alongside hundreds of interesting twitter users, to read their content at a glance. engage your social network with noteworthy links, photos and videos. discover which of your tweets were liked or retweeted.
create your free twitter account today!
twitter allows you to find interesting people or build a following of people who are interested in you. maintaining a social connection has never been easier!twitter allows celebs to build a personal connection with their fans. this is why twitter has become one of the most used social media platforms in the world.
build an engaging profile
customize your profile, add a photo, description, location, and background photo
tweet often and optimize your posting times
post visual content
use hashtags in your tweets
draw in followers outside of twitter
know what’s trending now
discover top trending hashtags and breaking news headlines. whether you’re interested in sports highlights, pop culture and entertainment or politics, twitter is your source of information.
live streaming events
join the conversation or watch live videos to deeply engage with large audiences directly from your mobile device. go live, create your own live streaming events, share videos or sit back and watch events from around the world.
privacy policy: twitter /en/privacy
terms and conditions: twitter /en/tos
原文: www blogs /and_he/archive/2011/05/23/2054494.html
转载留在以后参考~~
关于twitter这一块,自发这篇博文之后有很多人问我,有的验证成功了不跳转,或者其它原因什么的,我看了一下,这篇博文里面有写呀,下面以红色粗体文字注明一下
刚进公司,叫我先学习twitter和facebook,就类似于国内的微博,或者分享功能,点击某个按钮,出来一个提示框,可以分享到某些地方,这里实现的就是分享到twitter,当然得要使用代理,因为这是给老外做的,所以得符合他们的习惯
先说一下实现的功能吧,首先运行的时候,会检查是否登陆twitter(通过sharedpreference文件保存登陆状态),如果没有登陆的话会跳转到twitter的登陆认证页面,提示用户输入用户名和密码,这些就是oauth认证的步骤,不懂的看这儿 www blogs /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
=
"
api.twitter /oauth/request_token
"
;
public
static
final
string access_token_url
=
"
api.twitter /oauth/access_token
"
;
public
static
final
string callback_url
=
"
api.twitter /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
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 .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.getauthentication
"
&force_login=true
"
; }
catch
(twitterexception e) { e.printstacktrace();
return
"
baidu
"
; }
//
已经得到临时访问令牌
//
request token=7hmpofnr5ev1kjcw036mdi1hpvycbb1srkkk3r6ax30
//
request token secret=fahibfx04lbqhme392hth1lgl8hqxm2p0ij5kzlofk
//
url=
api.twitter /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
; 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.load;
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=
api.twitter /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()) .getprofileimage.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 mit(); 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.load;
//
载入网页
}
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); } }
最近接触的项目要实现twitter,facebook,email的分享功能。当然android可以简单的用intent调用第三方app实现这些功能。但是如果需求变态需要你自己写一个分享功能呢?
先介绍一下twitter的分享:
twiter这里我使用了twitter4j-core-android-2.2.6.jar这个是给android用的twitter4j的精简包,体积小。
授权(oauth)流程如下:
1) 注册申请twitter的开发者,创建app,获得开发者的key和secret,即consumer_key & consumer_secret
2) 用http通过twitter提供的api url 参数consumer_key & consumer_secret到twitter的服务器获取request_token
3) 用http通过twitter提供的api url 参数request_token到twitter服务器请求授权
4) 授权确认后页面跳转到由你之前注册时填写的callback_url为地址前缀的的一个新网址(页面),并获取到oauth_verifer
5) 最后,再发送request_token & oauth_verifer到twitter服务器换取access_token &?access_token_secret, 有了这两个值就可以通过它们进行各种twitter操作。
以上是授权的一个通用过程,但是在实现中需要考虑一些问题,因此有以下流程:
1\ 从手机的文件存储中读取access_token &?access_token_secret,,如果没有继续2\,有就直接设置它们到twitter对象中,然后到5\
2\ 进行授权,前提是要已经设置consumer_key & consumer_secret到了twitter对象中,向服务器获取request_token(代码实现中是要求传递call_back_url作为参数)
3\ 获取到request_token后将request_token作为参数传递并开启一个有webview的activity,然后自己用浏览器处理获取oauth_verifer,并将其作为参数传回给之前的那个activity
这里说一下用一个带webview 的activity处理的好处,一个是在有多个第三方浏览器的时候直接用intent会没办法关闭第三方浏览器,不过其实最关键的是我在使用系统自带的浏览器的获取oauth_verifierr时,虽然xml有写回调数据的处理(
4\ 在之前的activity里用接收到的oauth_verifer继续处理,向服务器获取access_token &?access_token_secret,并且设置到twitter对象中,并且把它存入文件
5\ 用twitter对象进行一些twitter操作
注意:在5\ 中操作时会遇到如果用户撤销了授权的一些情况,这个时候就会抛出异常,此时就需要从2\开始再一次进行授权。
关键的代码:
在oncreate()里初始化以及设置组件:
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_twitter);
mprefs = app.getinstance().getappprefs(); //获得操作文件系统的对象
initializetwitter(); //初始化twitter对象
intent intent = getintent();
if (intent != null) {
preview_url = intent.getstringextra("preview_url"); //intent传过来的一个字符串,把它作为推文
}
string accesstoken = mprefs.gettwitteraccesstoken();
string accesstokensecret = mprefs.gettwitteraccesstokensecret();
if (accesstoken == null || accesstokensecret == null) { //从文件系统里读取的access_token 和 access_token_secret的判断
reoauth(); //验证以及出问题的重复验证都会用这个函数
} else {
twitter.setoauthaccesstoken(new accesstoken(accesstoken, accesstokensecret));
}
close = (button)findviewbyid(r.id.btn_close);
send = (button)findviewbyid(r.id.btn_send);
content = (edittext)findviewbyid(r.id.edit_content); //在这个activity上用于显示推文内容
close.setonclicklistener(closelistener); //关闭,调用finish()
send.setonclicklistener(sendlistner); //发布推文
}
初始化twitter对象的代码:
private void initializetwitter() {
configurationbuilder builder = new configurationbuilder();
builder.setoauthconsumerkey(appconstants.twitter_consumer_key); //consumer_key
builder.setoauthconsumersecret(appconstants.twitter_consumer_secret); //consumer_secret
configuration conf = builder.build();
twitter = new twitterfactory(conf).getinstance();
twitter.setoauthaccesstoken(null);
}
reoauth 验证函数
private void reoauth() {
try {
twitter.setoauthaccesstoken(null);
requesttoken = twitter.getoauthrequesttoken(appconstants.twitter_callback_url); //获取requesttoken
} catch (twitterexception e) {
log.d("test", e.getmessage());
}
if (requesttoken != null) {
intent i = new intent(this, websiteactivity.class);
i.putextra(appconstants.extra_url, requesttoken.getauthentication);
startactivityforresult(i, titter_oauth); //开启一个带webview的activity处理,名字为websiteactivity
}
}
在websiteactivity里的处理关键动作:
private class mywebviewclient extends webviewclient { //要求是可以点击其他超练级继续浏览网页的,因此继承webviewclient实现
@override
public boolean shouldoverrideurlloading(webview view, string url) {
view.load;
return true;
}
@override
public void onpagefinished(webview view, string url) { //在网页加载结束后,对callback_url带回的参数进行获取。并关闭这个activity
super.onpagefinished(view, url);
progressbar.setvisibility(view.gone);
if (url != null && url.startswith(appconstants.twitter_callback_url)) {
string oauthverifer = uri.parse(url).
getqueryparameter(appconstants.twitter_oauth_verifier);
intent i = new intent();
i.putextra(appconstants.twitter_oauth_verifier, oauthverifer);
setresult(result_ok, i); //把获取的oauth_verifer
finish();
}
}
@override
public void onpagestarted(webview view, string url, bitmap favicon) {
super.onpagestarted(view, url, favicon);
progressbar.setvisibility(view.visible);
}
}
对返回的oauth_verifer进行处理,并与request_token一起去获取access_token & access_token_secret:
@override
protected void onactivityresult(int requestcode, int resultcode, intent data) {
super.onactivityresult(requestcode, resultcode, data);
log.d("test", "twitter onactivityresult()");
if (requestcode == titter_oauth) {
if (resultcode == activity.result_ok) {
oauthverifier = data.getstringextra(appconstants.twitter_oauth_verifier);
try {
accesstoken at = twitter.getoauthaccesstoken(requesttoken, oauthverifier);
string accesstoken = at.gettoken();
string accesstokensecret = at.gettokensecret();
twitter.setoauthaccesstoken(at); //这里要设置access_token
mprefs.settwitteraccesstoken(accesstoken); //记得存入文件系统
mprefs.settwitteraccesstokensecret(accesstokensecret);
} catch (twitterexception e) {
log.d("test", e.getmessage());
}
}
}
}
最后就可以用twitter发布推文了,send按钮事件:
private view.onclicklistener sendlistner = new view.onclicklistener() {
@override
public void onclick(view v) {
try {
twitter.updatestatus(content.gettext().tostring() preview_url);
} catch (twitterexception e) {
reoauth(); //在用户撤销授权或者删除app的时候,会抛出异常,这里就可以再次重新授权
log.d("test", e.getmessage());
}
}
};
还没有评论,来说两句吧...