投稿

4月, 2015の投稿を表示しています

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-iOS編その2

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-iOS編その1 の続き。 前回Twitterにログインしたので、今回はシングルツイートの取得とユーザタイムラインの取得をします。 シングルツイートの取得 std::string NativeLauncher::showTweet(){ NSURLRequest *request = [[[Twitter sharedInstance] APIClient] URLRequestWithMethod:@"GET" URL:@"https://api.twitter.com/1.1/statuses/show.json" parameters:@{@"id":@"xxxxxxxxxxxxxxxxxx"} error:nil]; NSURLResponse *response; NSError *err; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; std::string tweet_text = [[jsonObject valueForKey:@"text"] UTF8String]; return tweet_text; }; xxxxxxxxxxとなっている所にツイートIDを入れてください。 NSDataが帰ってくるので、NSJSONSerializationでNSDictionaryに変換します。 valueForKeyの"text"のところを変えると他の情報も取得できます。 他のパラメータや、データの構造は、 GET statuses/show/:id を見て

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-iOS編その1

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-Android編その4 の続き。 Twitterにログインするところまで。 Fabric のXcode用プラグインをインストール fabricユーザー登録から、プラグインのインストールまで このページが丁寧に説明してあって分かりやすいと思います。 すでにユーザ登録してある人は、そこは飛ばしてください。 プロジェクトにSDKを追加する [iOS] Fabric 入門 #1 アカウント登録 & Twitter Kit を使ってログインする 今度はこのページを見ながら。ネット上の資産は有効活用しましょう。 Run Script Build Phaseについては、公式 How to Add a Run Script Build Phase で解説されています。 Run Scriptを追加した後に、以前NativeLauncher.hで宣言した関数が無いとビルドできないので、 NativeLauncher.mmに #include "NativeLauncher.h" void NativeLauncher::loginTwitter(){ }; std::string NativeLauncher::showTweet(){ std::string tweet_text; return tweet_text; }; std::vector<std::string> NativeLauncher::showUserTimeline(){ std::vector<std::string> userTimeline_text; return userTimeline_text; }; と空の関数を用意しておきます。 Twitterにログインする AppDelegate.mに記述ところはCocosだとAppDelegateがcppファイルなので、NativeLauncher.mmのloginTwitter()に書きます。 TwitterLoginButtonもNativeLauncher.mmのloginTwitter()に書きますが、viewには

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-Android編その4

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-Android編その3 の続き ではいよいよ最終目標のユーザタイムラインの取得をします。 今回もまたAppActivityでメソッドを作ってNativeLauncherにjniを追加します。 AppActivity.java static String userTimeline_text[]; public static String[] showUserTimeline() throws InterruptedException{ final CountDownLatch cdl = new CountDownLatch(1); final int count = 5; StatusesService statusesService = Twitter.getApiClient().getStatusesService(); statusesService.userTimeline(null,null,count, null, null, true, false, true, true, new Callback<List<Tweet>>() { @Override public void success(Result<List<Tweet>> listResult) { String text[] = new String[count]; int tweetNum = 0; for (Tweet tweet : listResult.data) { text[tweetNum] = tweet.text; tweetNum++; } userTimeline_text = text; cdl.countDown(

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-Android編その3

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-Android編その2 の続き。 前回Twitterにログイン出来たので、 Rest API が使えるようになります。 今回はシングルツイートの取得をします。 AppActivity.java AppActivityでツイートのテキストを返すメソッドを作ります。 まずはコード static String tweet_text; public static String showTweet() throws InterruptedException{ final CountDownLatch cdl = new CountDownLatch(1); tweet_text = null; StatusesService statusesService = Twitter.getApiClient().getStatusesService(); statusesService.show(xxxxxxxxxxxxxxxxxL, null, null, null, new Callback<Tweet>() { @Override public void success(Result<Tweet> result) { tweet_text = result.data.text; cdl.countDown(); } public void failure(TwitterException exception) { System.out.println("failure"); } } ); cdl.await(); return tweet_text; } xxx

Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-Android編その2

イメージ
Cocos2d-xとTwitter Kitを使ってOAuth認証&ユーザタイムライン取得-Android編その1 の続き。 今回は前回作ったloginTwitter()をjniを使ってcocos2d-xから呼び出します。 jniを使える環境を整える まずはjniを使えるようにします。 Xcodeでやったほうが楽だと思います。 ここのサイトがとてもわかり易いので見てください。 cocos2d-xでiOSとAndroidの処理を分ける ※Android.mkはproj.android>jniに有ります。 jniを使ってloginTwitter()を呼び出す cocos2d-xでjniを使ってみる を参考にしながら進めていきます。 jniを使ったことがある人は特に迷うことは無いと思います。 NativeLauncher.h #ifndef helloWorld_NativeLauncher_h #define helloWorld_NativeLauncher_h class NativeLauncher{ public: static void loginTwitter(); }; #endif NativeLauncher.cpp #include <jni.h> #include "NativeLauncher.h" #include "platform/android/jni/JniHelper.h" using namespace cocos2d; #define CLASS_NAME "org.cocos2dx.cpp.AppActivity" void NativeLauncher::loginTwitter(){ JniMethodInfo JMI; if (JniHelper::getStaticMethodInfo(JMI, CLASS_NAME, "loginTwitter", "()V")) { JMI.env->CallStaticVoidMethod(JMI.classID, JMI.methodID); JMI