我正在尝试使用twitter 4j加载twitter页面但是没有加载twitter页面我已经注册了我的应用程序并获取了我的消费者密钥和消费者密钥这里是我的代码
package a.a.a;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
public class StatusListActivity extends ListActivity{
private Otweet app;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
app=(Otweet)getApplication();
setContentView(R.layout.main);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(!app.isAuthorized())
{
beginAuthorization();
}
else
{
}
}
public void beginAuthorization()
{
Intent intent=new Intent(StatusListActivity.this,AuthorizationActivity.class);
startActivity(intent);
}
}
这是授权活动
package a.a.a;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class AuthorizationActivity extends Activity{
private Otweet app;
WebView WV;
Twitter twitter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
app=(Otweet)getApplication();
setContentView(R.layout.authorization_view);
WV=(WebView)findViewById(R.id.web_view);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//twitter=new TwitterFactory().getInstance();
//twitter.setOAuthConsumer("C6lqBhtUcC5f9wuauwoxag", "BttswrGyCxaeLgOZAdtfSqlbEg1uiw6xnuXD4y2rcjE");
String authURL=app.BeginAuthorization();
WV.loadUrl(authURL);
}
}
这是otweet活动
public class Otweet extends Application {
private OAuthHelper authhelper;
private Twitter twitter;
private RequestToken currentRequestToken;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
authhelper=new OAuthHelper(this);
twitter=new TwitterFactory().getInstance();
authhelper.configureOAuth(twitter);
}
public boolean isAuthorized()
{
return authhelper.hasAccessToken();
}
public String BeginAuthorization()
{
try
{
if(null==currentRequestToken)
{
currentRequestToken=twitter.getOAuthRequestToken();
}
return currentRequestToken.getAuthorizationURL();
}
catch (TwitterException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
函数begin authorization总是返回null。
答案 0 :(得分:0)
尝试替换
authhelper.configureOAuth(twitter)
与
twitter.setOAuthConsumer(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
如果这不起作用,请尝试TwitterOAuthView。
使用TwitterOAuthView,您不必关心OAuth流程。只需致电
view.start(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL, true, listener);
并通过下面定义的TwitterOAuthView.Listener接口接收结果。
void onSuccess(TwitterOAuthView view, AccessToken accessToken);
void onFailure(TwitterOAuthView view, TwitterOAuthView.Result result);