【发布时间】:2014-03-14 19:31:47
【问题描述】:
我正在使用 twitter4j 将 twitter 集成到 android 中。 这是我的推特代码。我正在为 twitter 创建单独的类。
我查看了与此类似的问题,但对我没有任何帮助。 Question 和其他一些问题。
Twitter 类
public class OBLTwitter {
private static final String TAG = "OBLTwitter";
private Activity activity;
// Twitter constants
private static final String TWITTER_CONSUMER_KEY = "KEY";
private static final String TWITTER_CONSUMER_SECRET = "SECRET";
public static final String TWITTER_CALLBACK_URL = "app://ridesharebuddy";
// Twitter variables
private static Twitter twitter;
private static RequestToken requestToken;
public static boolean userDeniedToContinue;
public OBLTwitter(Activity activity) {
Log.d(TAG, "Parameterized constructor called.");
this.activity = activity;
userDeniedToContinue = false;
}
// Login to twitter
public void loginToTwitter() {
Log.e(TAG, "Logging in to twitter.");
if(!isNetworkAvailable(this.activity))
{
Log.e(TAG, "Interent connection not available");
}
// Set up Twitter object
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
if(twitter == null)
{
Log.e(TAG, "twitter is null");
}
Log.e("called", "called run method");
// Get RequestToken and call authentication URL to show
// twitter page
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
Log.e(TAG, "getting request token");
Log.e("oAuth token :", requestToken.getToken());
Log.e("oAUth secret:", requestToken.getTokenSecret());
Log.e("oAuth URL: ", requestToken.getAuthenticationURL());
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(requestToken.getAuthenticationURL())));
} catch (TwitterException te) {
Log.e(TAG, "Twitter exception, Login error.");
te.printStackTrace();
Log.e(TAG, "Error code : " + te.getErrorCode());
Log.e(TAG, "Error message : " + te.getErrorMessage());
Log.e(TAG, "Status code : " + te.getStatusCode());
Log.e(TAG, "Access level : " + te.getAccessLevel());
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
public boolean isNetworkAvailable(Context context)
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}
这是我调用 loginToTwitter 函数的活动代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OBLTwitter twitter = new OBLTwitter(this);
twitter.loginToTwitter();
}
这是我的 manifest 文件,我添加这个是因为我做了一些不同问题的回答者建议的更改。
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.objectlounge.OBLTwitter.MainActivity"
android:label="@string/app_name" >
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="ridesharebuddy"
android:scheme="app" />
</intent-filter>
</activity>
</application>
错误日志
【问题讨论】:
-
我在这里发布了一些很好的示例代码github.com/ericwoodruff/android_twitter_oauth
-
感谢您。我下载了供参考。我试图运行它,但它在 onActivityResult 中给了我错误。它进入开关部分的取消块,然后崩溃。它没有显示我登录并显示已取消。
-
它可能会假设您已经创建了一个 twitter4j.properties 并将其与您的秘密放在类路径中。 twitter4j.org/en/configuration.html
-
我删了,还是一样的问题。
-
androidhive.info/2012/09/android-twitter-oauth-connect-tutorial 请查看此链接,它将为您提供 100% 的帮助