如何在Android 4.0上集成Twitter。应用

时间:2012-12-17 16:16:45

标签: twitter android-4.0-ice-cream-sandwich

我刚刚使用twitter4j开发了针对Android 2.3的twitter集成应用程序,但现在我想将此应用程序用于Android 4.0,我必须更改我的应用程序,因为它不起作用,

我想导入mu currentUset变量,例如:screen_name,user_id和profile_image_url!然后我只想导入我的粉丝和粉丝。

是的,有人能帮帮我吗?我被贬低了!我很瞪眼,但我对此没有任何答案。

2 个答案:

答案 0 :(得分:0)

在main_activity中添加此代码

private TwitterApp mTwitter;
private static final String CONSUMER_KEY = "your consumer key"; 
private static final String CONSUMER_SECRET = "your consumer secret key"; 

private enum FROM {
    TWITTER_POST, TWITTER_LOGIN
};

private enum MESSAGE {
    SUCCESS, DUPLICATE, FAILED, CANCELLED
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTwitter = new TwitterApp(this, CONSUMER_KEY, CONSUMER_SECRET);
}

public void onClick(View v) {
    mTwitter.setListener(mTwLoginDialogListener);
    mTwitter.resetAccessToken();
    if (mTwitter.hasAccessToken() == true) {
        try {
            mTwitter.updateStatus(TwitterApp.MESSAGE);
            postAsToast(FROM.TWITTER_POST, MESSAGE.SUCCESS);
        } catch (Exception e) {
            if (e.getMessage().toString().contains("duplicate")) {
                postAsToast(FROM.TWITTER_POST, MESSAGE.DUPLICATE);
            }
            e.printStackTrace();
        }
        mTwitter.resetAccessToken();
    } else {
        mTwitter.authorize();
    }
}

private void postAsToast(FROM twitterPost, MESSAGE success) {
    switch (twitterPost) {
    case TWITTER_LOGIN:
        switch (success) {
        case SUCCESS:
            Toast.makeText(this, "Login Successful", Toast.LENGTH_LONG)
                    .show();
            break;
        case FAILED:
            Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
        default:
            break;
        }
        break;
    case TWITTER_POST:
        switch (success) {
        case SUCCESS:
            Toast.makeText(this, "Posted Successfully", Toast.LENGTH_LONG)
                    .show();
            break;
        case FAILED:
            Toast.makeText(this, "Posting Failed", Toast.LENGTH_LONG)
                    .show();
            break;
        case DUPLICATE:
            Toast.makeText(this,
                    "Posting Failed because of duplicate message...",
                    Toast.LENGTH_LONG).show();
        default:
            break;
        }
        break;
    }
}

private TwDialogListener mTwLoginDialogListener = new TwDialogListener() {

    @Override
    public void onError(String value) {
        postAsToast(FROM.TWITTER_LOGIN, MESSAGE.FAILED);
        Log.e("TWITTER", value);
        mTwitter.resetAccessToken();
    }

    @Override
    public void onComplete(String value) {
        try {
            mTwitter.updateStatus(TwitterApp.MESSAGE);
            postAsToast(FROM.TWITTER_POST, MESSAGE.SUCCESS);
        } catch (Exception e) {
            if (e.getMessage().toString().contains("duplicate")) {
                postAsToast(FROM.TWITTER_POST, MESSAGE.DUPLICATE);
            }
            e.printStackTrace();
        }
        mTwitter.resetAccessToken();
    }
};

您可以从Here继续 希望它对你有用

答案 1 :(得分:0)

您可以使用Twitter Helper将Twitter集成到Android应用中。它非常简单。