Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AndroidアプリでNetworkOnMainThreadExceptionを避けつつTwitter4Jを使用したアプリ連携を行う #Twitter4J ref: http://qiita.com/rsato/items/39efd16c8a77a893a67e
// 準備
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
twitter.setOAuthAccessToken(null);
RequestToken requestToken = twitter.getOAuthRequestToken(); // ここで通信
// このURLでブラウザで認証
String url = requestToken.getAuthorizationURL();
// PIN入力
AccessToken accessToken = twitter.getOAuthAccessToken(pin); // ここでも通信
// トークンの取得
String token = accessToken.getToken();
String tokenSecret = accessToken.getTokenSecret();
TwitterOAuthRequestTokenCallbacks oAuthRequestTokenCallbacks = new TwitterOAuthRequestTokenCallbacks(this, mTwitter);
getLoaderManager().initLoader(0, null, oAuthRequestTokenCallbacks);
// 準備
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
twitter.setOAuthAccessToken(null);
RequestToken requestToken = twitter.getOAuthRequestToken(); // ここで通信
// このURLでブラウザで認証
String url = requestToken.getAuthorizationURL();
// PIN入力
AccessToken accessToken = twitter.getOAuthAccessToken(pin); // ここでも通信
// トークンの取得。これらを保管しておく。
String token = accessToken.getToken();
String tokenSecret = accessToken.getTokenSecret();
public class TwitterOAuthRequestTokenLoader extends AsyncTaskLoader<RequestToken> {
private Twitter mTwitter;
public TwitterOAuthRequestTokenLoader(Context context, Twitter twitter) {
super(context);
mTwitter = twitter;
}
@Override
public RequestToken loadInBackground() {
RequestToken requestToken = null;
try {
requestToken = mTwitter.getOAuthRequestToken();
} catch (TwitterException e) {
requestToken = null;
}
return requestToken;
}
}
public class TwitterOAuthRequestTokenCallbacks implements LoaderCallbacks<RequestToken> {
private Context mContext;
private Twitter mTwitter;
public TwitterOAuthRequestTokenCallbacks(Context context, Twitter twitter) {
mContext = context;
mTwitter = twitter;
}
@Override
public Loader<RequestToken> onCreateLoader(int id, Bundle args) {
Loader<RequestToken> loader = new TwitterOAuthRequestTokenLoader(mContext, mTwitter);
loader.startLoading();
return loader;
}
@Override
public void onLoadFinished(Loader<RequestToken> arg0, RequestToken requestToken) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthorizationURL()));
mContext.startActivity(intent);
}
}
mTwitter = new TwitterFactory().getInstance();
mTwitter.setOAuthConsumer(KEY), KEY_SECRET);
mTwitter.setOAuthAccessToken(null);
LoaderManager.LoaderCallbacks<RequestToken> requestTokenCallbacks = new TwitterOAuthRequestTokenCallbacks(this, mTwitter);
getSupportLoaderManager().initLoader(0, null, requestTokenCallbacks);
mTwitter = new TwitterFactory().getInstance();
mTwitter.setOAuthConsumer(getString(R.string.twitter_consumer_key), getString(R.string.twitter_consumer_key_secret));
mTwitter.setOAuthAccessToken(null);
public class TwitterOAuthRequestTokenCallbacks implements LoaderCallbacks<RequestToken> {
private Context mContext;
private Twitter mTwitter;
public TwitterOAuthRequestTokenCallbacks(Context context, Twitter twitter) {
mContext = context;
mTwitter = twitter;
}
@Override
public Loader<RequestToken> onCreateLoader(int id, Bundle args) {
TwitterOAuthRequestTokenLoader loader = new TwitterOAuthRequestTokenLoader(mContext, mTwitter);
loader.forceLoad();
return loader;
}
@Override
public void onLoadFinished(Loader<RequestToken> arg0, RequestToken requestToken) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthorizationURL()));
mContext.startActivity(intent);
}
}
public class TwitterOAuthRequestTokenLoader extends AsyncTaskLoader<RequestToken> {
private Twitter mTwitter;
public TwitterOAuthRequestTokenLoader(Context context, Twitter twitter) {
super(context);
mTwitter = twitter;
}
@Override
public RequestToken loadInBackground() {
RequestToken requestToken = null;
try {
requestToken = mTwitter.getOAuthRequestToken();
} catch (TwitterException e) {
requestToken = null;
}
return requestToken;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.