Sample Project to use Facebook, twitter, and send e-mails using cocos2d-x


SAMPLE LINK AT END OF THIS MESSAGE


I´m developing a game in cocosd2-x since 2 month. I know that post scores on twitter or facebook is important, and i wan to implement it. Someone know if exist a extension or singleton structure to do it? I have tried, but is not easy or fast, it will take me a lot of time to get a good structure. I have been looking for in internet, and i get:

Facebook:

But i don´t know how to compile on linux, is not a quick solution.

And i have the same problem with: http://code.google.com/p/facebook-cpp-graph-api/

Twitter:

http://code.google.com/p/twitcurl/
https://dev.twitter.com/discussions/3287

Some idea / code?
Is going cocos2d-x to include something similar in the library?


WORKING SAMPLE:

https://github.com/Piperoman/CCSocialNetwork.git

Hi,
for facebook I used: http://www.android10.org/index.php/articleslibraries/290-facebook-integration-in-your-android-application
if you find this difficult I can help you, just write me back.

for twitter I do simply:

String tweetUrl = "https://twitter.com/intent/tweet?text=Hello ! I have just got " + score + " points in mygame for Android !!!!";
                        Uri uri = Uri.parse(tweetUrl);
                        startActivity(new Intent(Intent.ACTION_VIEW, uri));

and it works :slight_smile:
Cheers !
Barti

2 Likes

Thanks for your response.

The point is, i´m using cocos2d-x to develop for iphone and android together.

A solution is implement it for android, and for iOS separated.

But the optimal solution is a c++ API integrated to use it directly from a GameManager.cpp in the code.

If i find anything, i will do it how you said :frowning:

You simply can’t AFAIK implement FB an Twitter in your app both for iOS and Android together :slight_smile:
You should use something like:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    //android part code here
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 
    //iOS part code here
#endif

I develop also for iOS and Android both, but things like FB, Twitter, OpenFeint you must do separately (especially that to implement it on Android you sould use JNI to connect with Java part).
Cheers,
Barti

I think it could be possible, i took a look to http://code.google.com/p/facebook-cpp-graph-api/, and i think is using HTTP to validate the session, and store the keys and token in the cpp code.

Other option could be use curl, to make the calls, there are a post right here: http://www.cocos2d-x.org/boards/18/topics/11703 that talk about using http request.

Hi Pipero,

Did you finally get facebook API working both on android and ios with the same code?

No, no yet. I´m developing my game right now, i hope someone help with that API. If not, i will try to do it later.

I gonna try implement an singleton using:

#if (CC_TARGET_PLATFORM CC_PLATFORM_ANDROID)
//android part code here
#elif (CC_TARGET_PLATFORM CC_PLATFORM_IOS)
//iOS part code here
#endif

but following http://www.android10.org/index.php/articleslibraries/290-facebook-integration-in-your-android-application if Android, with JNI code added on eclipse project, and follow another tutorial on iOS.

I will post if it a succesful.

Hi Barti,

I need some help to implement Facebook con android using cocos2d-x. It’s working fine in iOS, but there aren’t tutorials in android. I have followed http://www.android10.org/index.php/articleslibraries/290-facebook-integration-in-your-android-application , and its launch the app. But i don ’t know how to call it using JNI from cocos2d-x, and how to display the Intent calling postMessageOnWall.

Can you help me a bit?

Thanks

P.D I will try to update the both projects to GitHub using cocos2d-x if this work, because is very difficult to new developers to do this.

Hi,
here’s code that I used mostly for C**/Java integration: http://blog.molioapp.com/2011/11/openfeint-and-admob-integrated-with.html
There is also JNI code, which I think is quite intuitive. You simply call JNI method from C**, which invokes Java method i.e. postMessageOnWall by some:
@ mid = env->GetStaticMethodID(mClass, “showDashBoards”, “()V”); @
Read about JNI method invocation here: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html
If you would have any problems write, and I’ll try to help you.
Cheers,
Barti

Barti Sid wrote:

Hi,
here’s code that I used mostly for C**/Java integration: http://blog.molioapp.com/2011/11/openfeint-and-admob-integrated-with.html
There is also JNI code, which I think is quite intuitive. You simply call JNI method from C**, which invokes Java method i.e. postMessageOnWall by some:
@ mid = env->GetStaticMethodID(mClass, “showDashBoards”, “()V”); @
Read about JNI method invocation here: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html
If you would have any problems write, and I’ll try to help you.
Cheers,
Barti

Update: I wrote little tutorial for this issue: http://lipekdev.wordpress.com/2012/09/19/integrating-facebook-twitter-open-feint-other-with-cocos2d-x-in-android-project/

Hi Barti.

I have been some problems with calls from C++ to Java, i followed your tutorial, but i get an exception trying to call a post in facebook’s wall. I’m working on it.

Actually just work on iOS. No in Android, but facebook libraries are linked on the project and close to work ^^.

This is the code:

https://github.com/Piperoman/CCSocialNetwork

If someone can help us to fix it… he will be welcome.
If not, don’t worry, i will fix it some day.

Hi ,
I am also facing some problems with the calls from C*+ to Java.
If you got some solution please share it .
Pipero Man wrote:

Hi Barti.
>
I have been some problems with calls from C*+ to Java, i followed your tutorial, but i get an exception trying to call a post in facebook’s wall. I’m working on it.
>
Actually just work on iOS. No in Android, but facebook libraries are linked on the project and close to work ^^.
>
This is the code:
>
https://github.com/Piperoman/CCSocialNetwork
>
If someone can help us to fix it… he will be welcome.
If not, don’t worry, i will fix it some day.

What is your problem? I cannot help you if you don´t explain it!

The code works on my computer, to run the project you must put all the data correctly to compile.

Actually I am new to cocos2dx and i also want to do facebook integration which will work for both android and ios.
For ios , i know the way but for android i dont no anything.
I searched for it and found out that i need to use JNI for calling functionality from java to c++ code but i am not able to find a good tutorial for it.
If you have done the integration with both of them so please guide me how to do that .

Pipero Man wrote:

What is your problem? I cannot help you if you don´t explain it!
>
The code works on my computer, to run the project you must put all the data correctly to compile.

Just do a clone from https://github.com/Piperoman/CCSocialNetwork

Change the path of the makefile to compile and try it working.

Then just take a look to the code to see how to do C++ call to java.

It is difficult to do because there are not tutorials…. I hope this example can help you. If you have a problem just ask.

I am able to compile your code but its crashing.

Steps i followed to make your project compiled:-

  1. Open eclipse and make new Android project with existing code.
  2. Browse your project and open it .
  3. I added the facebook lib.
  4. Classes folder doesnt have the classes into it so i linked all the classes from that folder.
  5. There is no error so i tried to compiled it and was successfully able to do that .
  6. It ran on the device , i was able to see the screen where there are 3 options i.e. mail,twitter and fb.
  7. I clicked on FB and it crashed.

Sorry for explaining in that detail but i am very new to this platform and want to know what i did wrong .
I check the Android.mk file , the paths are correct.

Error i got is :
10-06 01:46:25.797: E/AndroidRuntime(9947): FATAL EXCEPTION: GLThread 11
10-06 01:46:25.797: E/AndroidRuntime(9947): java.lang.ExceptionInInitializerError
10-06 01:46:25.797: E/AndroidRuntime(9947): at com.facebook.android.Facebook.autoPublishAsync(Facebook.java:1049)
10-06 01:46:25.797: E/AndroidRuntime(9947): at com.facebook.android.Facebook.authorize(Facebook.java:222)
10-06 01:46:25.797: E/AndroidRuntime(9947): at com.facebook.android.Facebook.authorize(Facebook.java:147)
10-06 01:46:25.797: E/AndroidRuntime(9947): at tutorial.getsocial.meizpipero.FacebookConnector.login(FacebookConnector.java:42)
10-06 01:46:25.797: E/AndroidRuntime(9947): at tutorial.getsocial.meizpipero.GetSocial.postMessage(GetSocial.java:201)
10-06 01:46:25.797: E/AndroidRuntime(9947): at org.cocos2dx.lib.Cocos2dxRenderer.nativeTouchesEnd(Native Method)
10-06 01:46:25.797: E/AndroidRuntime(9947): at org.cocos2dx.lib.Cocos2dxRenderer.handleActionUp(Cocos2dxRenderer.java:79)
10-06 01:46:25.797: E/AndroidRuntime(9947): at org.cocos2dx.lib.Cocos2dxGLSurfaceView$10.run(Cocos2dxGLSurfaceView.java:348)
10-06 01:46:25.797: E/AndroidRuntime(9947): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1295)
10-06 01:46:25.797: E/AndroidRuntime(9947): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
10-06 01:46:25.797: E/AndroidRuntime(9947): Caused by: java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()
10-06 01:46:25.797: E/AndroidRuntime(9947): at android.os.Handler.(Handler.java:121)
10-06 01:46:25.797: E/AndroidRuntime(9947): at android.os.AsyncTask$InternalHandler.(AsyncTask.java:421)
10-06 01:46:25.797: E/AndroidRuntime(9947): at android.os.AsyncTask$InternalHandler.(AsyncTask.java:421)
10-06 01:46:25.797: E/AndroidRuntime(9947): at android.os.AsyncTask.(AsyncTask.java:152)
10-06 01:46:25.797: E/AndroidRuntime(9947): … 10 more

Pipero Man wrote:

Just do a clone from https://github.com/Piperoman/CCSocialNetwork
>
Change the path of the makefile to compile and try it working.
>
Then just take a look to the code to see how to do C++ call to java.
>
It is difficult to do because there are not tutorials…. I hope this example can help you. If you have a problem just ask.

Im not sure what is the problem, so i gonna explain to you the steps y follow.

1º Clone the project.
2º Compile using $PATH/GetSocial/proj.android/build_native.sh ; Must be correct
3º Open eclipse and IMPORT the project located on $PATH/Getsocial/proj.android
4º Open Facebook Libraries on $PATH/facebook-facebook-android-sdk-525f851
5º Add Path Facebook, both projects must be open
6º Run the project.

If all this steps are OK, then, you maybe must configure paths on projects. I can´t help without more info. Its important that you uncomment all CCLogs to see on ADB LOGCAT where the code is failing.

Report what Logs do you see and what logs you don´t, and i will help you to correct the problem.

Sorry for troubling you again but i followed all the steps you explained above but still its crashing when i click on share the on fb.

Actually email and twitter is working fine but there is some problem with fb.

Is post functionality is in GetSocial.java class??
Shall i try to modify it ??

Logs are
@
10-06 02:59:14.105: W/apk path(11609): /data/app/tutorial.getsocial.meizpipero-1.apk
10-06 02:59:14.125: D/cocos2d-x debug info(11609): Java_tutorial_getsocial_meizpipero_GetSocial_testCallFromJava
10-06 02:59:21.965: D/cocos2d-x debug info(11609): HelloWorld: try to send an email
10-06 02:59:21.965: D/cocos2d-x debug info(11609): Static postMessageEMail
10-06 02:59:21.965: D/cocos2d-x debug info(11609): Status: 0
10-06 02:59:21.965: D/cocos2d-x debug info(11609): jClass Located?
10-06 02:59:21.965: D/cocos2d-x debug info(11609): mID: 1101587796
10-06 02:59:21.965: D/activity(11609): Send a email on Java
10-06 02:59:21.965: D/PhoneWindow(11609): couldn’t save which view has focus because the focused view org.cocos2dx.lib.Cocos2dxGLSurfaceView@44c68958 has no id.
10-06 02:59:21.965: D/cocos2d-x debug info(11609): Finish
10-06 02:59:25.915: D/cocos2d-x debug info(11609): HelloWorld: try to send a tweet
10-06 02:59:25.915: D/cocos2d-x debug info(11609): Static postMessageToTweet
10-06 02:59:25.915: D/cocos2d-x debug info(11609): Status: 0
10-06 02:59:25.915: D/cocos2d-x debug info(11609): jClass
10-06 02:59:25.915: D/cocos2d-x debug info(11609): mID: 1101587900
10-06 02:59:25.915: D/activity(11609): Tweet on Java
10-06 02:59:25.945: D/PhoneWindow(11609): couldn’t save which view has focus because the focused view org.cocos2dx.lib.Cocos2dxGLSurfaceView@44c68958 has no id.
10-06 02:59:25.945: D/cocos2d-x debug info(11609): Finish
10-06 02:59:30.565: D/cocos2d-x debug info(11609): HelloWorld: try to post on Facebook
10-06 02:59:30.565: D/cocos2d-x debug info(11609): Static postMessageToFB
10-06 02:59:30.565: D/cocos2d-x debug info(11609): Status: 0
10-06 02:59:30.565: D/cocos2d-x debug info(11609): jClass Located?
10-06 02:59:30.565: D/cocos2d-x debug info(11609): mID: 1101587692
10-06 02:59:30.565: D/activity(11609): postMessage (FB) on Java
10-06 02:59:30.565: W/dalvikvm(11609): Exception Ljava/lang/RuntimeException; thrown during Landroid/os/AsyncTask;.
10-06 02:59:30.565: D/cocos2d-x debug info(11609): Finish
10-06 02:59:30.575: W/dalvikvm(11609): threadid=10: thread exiting with uncaught exception (group=0x40020ac0)
10-06 02:59:30.585: E/AndroidRuntime(11609): FATAL EXCEPTION: GLThread 11
10-06 02:59:30.585: E/AndroidRuntime(11609): java.lang.ExceptionInInitializerError
10-06 02:59:30.585: E/AndroidRuntime(11609): at com.facebook.android.Facebook.autoPublishAsync(Facebook.java:1049)
10-06 02:59:30.585: E/AndroidRuntime(11609): at com.facebook.android.Facebook.authorize(Facebook.java:222)
10-06 02:59:30.585: E/AndroidRuntime(11609): at com.facebook.android.Facebook.authorize(Facebook.java:147)
10-06 02:59:30.585: E/AndroidRuntime(11609): at tutorial.getsocial.meizpipero.FacebookConnector.login(FacebookConnector.java:42)
10-06 02:59:30.585: E/AndroidRuntime(11609): at tutorial.getsocial.meizpipero.GetSocial.postMessage(GetSocial.java:201)
10-06 02:59:30.585: E/AndroidRuntime(11609): at org.cocos2dx.lib.Cocos2dxRenderer.nativeTouchesEnd(Native Method)
10-06 02:59:30.585: E/AndroidRuntime(11609): at org.cocos2dx.lib.Cocos2dxRenderer.handleActionUp(Cocos2dxRenderer.java:79)
10-06 02:59:30.585: E/AndroidRuntime(11609): at org.cocos2dx.lib.Cocos2dxGLSurfaceView$10.run(Cocos2dxGLSurfaceView.java:348)
10-06 02:59:30.585: E/AndroidRuntime(11609): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1295)
10-06 02:59:30.585: E/AndroidRuntime(11609): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
10-06 02:59:30.585: E/AndroidRuntime(11609): Caused by: java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()
10-06 02:59:30.585: E/AndroidRuntime(11609): at android.os.Handler.(Handler.java:121)
10-06 02:59:30.585: E/AndroidRuntime(11609): at android.os.AsyncTask$InternalHandler.(AsyncTask.java:421)
10-06 02:59:30.585: E/AndroidRuntime(11609): at android.os.AsyncTask$InternalHandler.(AsyncTask.java:421)
10-06 02:59:30.585: E/AndroidRuntime(11609): at android.os.AsyncTask.(AsyncTask.java:152)
10-06 02:59:30.585: E/AndroidRuntime(11609): … 10 more
@

Pipero Man wrote:

Im not sure what is the problem, so i gonna explain to you the steps y follow.
>
1º Clone the project.
2º Compile using $PATH/GetSocial/proj.android/build_native.sh ; Must be correct
3º Open eclipse and IMPORT the project located on $PATH/Getsocial/proj.android
4º Open Facebook Libraries on $PATH/facebook-facebook-android-sdk-525f851
5º Add Path Facebook, both projects must be open
6º Run the project.
>
If all this steps are OK, then, you maybe must configure paths on projects. I can´t help without more info. Its important that you uncomment all CCLogs to see on ADB LOGCAT where the code is failing.
>
Report what Logs do you see and what logs you don´t, and i will help you to correct the problem.

I don ´t know whats is happening.
It throw an exception OpenGL using FB.

What is your android version??? I was using Android 3.0, with 2.2 minimun and cocos2d-x 2.0.1

Try to post it on www.stackoverflow.com. If you get a answer, please post it here!