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;
    }

xxxxxxxxxxの所にツイートidが入ります。何故か最後にLをつけるようです。
nullになっているパラメータの内容はGET statuses/show/:idを見てください。

ほかは基本的にAccess Twitter's REST APIと同じですが、CountDounLatchを使ってレスポンスが帰ってくるのを待っています。

result.dataにデータが格納されています。GET statuses/show/:idにデータ構造が載っているので、ここを見れば色々な情報を取り出せます。
例えば"result.data.createdAt"とすればツイート日時が取得できます。

NativeLauncher


jniを使ってAppActivityのshowTweet()を呼び出します。
NativeLauncher.h
#include <string.h>
#include "cocos2d.h"

using namespace std;
using namespace cocos2d;

class NativeLauncher{
public:
    static void loginTwitter();
    static std::string showTweet();
};

NativeLauncher.cpp
#include <jni.h>
#include "NativeLauncher.h"
#include <string>
#include "platform/android/jni/JniHelper.h"
using namespace cocos2d;

#define CLASS_NAME "org.cocos2dx.cpp.AppActivity"
std::string NativeLauncher::showTweet(){
    std::string tweet_text;
    JniMethodInfo JMI;
    if (JniHelper::getStaticMethodInfo(JMI, CLASS_NAME, "showTweet", "()Ljava/lang/String;")) {
        jstring jStr = (jstring)JMI.env->CallStaticObjectMethod(JMI.classID, JMI.methodID);
        tweet_text =JMI.env->GetStringUTFChars(jStr, NULL);
        JMI.env->DeleteLocalRef(JMI.classID);
    }
    return tweet_text;
}

戻り値があるメソッドを呼ぶ場合のjniの書き方はcocos2d-xでjniを使ってみるの下の方を見てください。

あとはcocosからNativeLauncher::showTweet();を呼ぶだけです。
出来ましたでしょうか。

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

コメント

このブログの人気の投稿

2次元配列のコピー::JavaScript

iframeの利点と欠点

chromyを実行するとエラー:"Failed to launch a browser"が出る