2012-07-10 181 views
0

我試圖使用Twitter 4J加載Twitter頁面,但沒有加載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; 
    } 

} 

函數開始授權總是返回null。

回答

0

嘗試用

twitter.setOAuthConsumer(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET); 

更換

authhelper.configureOAuth(twitter) 

如果這不起作用,嘗試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);