可移植:如何使用android原生facebook adk



我们正在尝试使用android原生的facebooksdk来发布facebookwallpost。

这里我们使用的是像这样的Bridge类

.h文件

#ifdef ANDROID
#import <BridgeKit/JavaObject.h>
#import <BridgeKit/AndroidActivity.h>
@interface BridgeKitExt : JavaObject
- (id)initWithString:(NSString*)str;
- (void)loginFacebook:(AndroidActivity*)activity;
@end
#endif

和像这个一样的.m文件

#ifdef ANDROID
#import "BridgeKitExt.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-property-implementation"
#pragma clang diagnostic ignored "-Wincomplete-implementation"
@implementation BridgeKitExt
+ (void)initializeJava
{
    [super initializeJava];
    [BridgeKitExt registerConstructorWithSelector:@selector(initWithString:)
                                    arguments:[NSString className], nil];

    [BridgeKitExt registerInstanceMethod:@"loginFacebook"
                            selector:@selector(loginFacebook:)
                           arguments:@"android/app/Activity", nil];
}
+ (NSString *)className
{
    return @"com.duksel.apportable.BridgeKitExt";
}
@end
#pragma clang diagnostic pop
#endif

BridgeKitExt.java文件类似于这个

package com.duksel.apportable;
import java.util.Arrays;
import java.util.List;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.provider.Settings;
//import android.util.Log;
import android.view.*;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
//import android.widget.Toast;
import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionLoginBehavior;
import com.facebook.SessionState;
import com.facebook.Session.Builder;
import com.facebook.Session.OpenRequest;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;
public class BridgeKitExt extends Activity
{
//    private static final String TAG = "BridgeKitExt";
private String initMessage = "";
private String SHARE_MESSAGE="sample text";
private boolean pendingPublishStory = false;
// For Facebook
private static final String TAG = "Facebook";
private static final String KEY_FB_SHARE = "fb_share";
private static final String STORY_PUBLISH_KEY = "story_required_publish";
public void postFacebookWall(String message) {
    SHARE_MESSAGE = message;
    publishStory();
}
private void publishStory() {
    Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        Bundle bundle = new Bundle();
        bundle.putString("message", SHARE_MESSAGE);
        //bundle.putString("link", Purchase_Detail.link);
        //bundle.putString("picture", ICON_LINK);
        Request.Callback callback = new Request.Callback() {
            @Override
            public void onCompleted(Response response) {
                GraphObject object = response.getGraphObject();
                if (object != null) {
                    JSONObject jsonResponse = object.getInnerJSONObject();
                    String postId = "";
                    try {
                        postId = jsonResponse.getString("id");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
//                          Toast(error.getErrorMessage());
                    } else {
//                          Toast("Post Succsessfully Done");
                    }
                } else {
//                      Toast("Error in Posting");
                }
            }
        };
        Request request = new Request(session, "me/feed", bundle,HttpMethod.POST, callback);
        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    } else {
        pendingPublishStory = true;
        loginFacebook();
    }
}
private void onSessionStateChange(Session session, SessionState state,
                                  Exception exception) {
    if (state.isOpened()) {
//          Log("onSessionStateChange " + state.toString());
        makeMeRequest(session);
        if (pendingPublishStory) {
            pendingPublishStory = false;
            publishStory();
        }
    } else if (state.isClosed()) {
//          Log("onSessionStateChange " + state.toString());
    }
}
private Session.StatusCallback statusCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
                 Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data==null)
return;
}
}
@Override
protected void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(STORY_PUBLISH_KEY, pendingPublishStory);
}
private void makeMeRequest(final Session session) {
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (session == Session.getActiveSession()) {
if (user != null) {
//nothing here
}
}
if (response.getError() != null) {
//Log("Error : newMeRequest");
}
}
});
request.executeAsync();
}
protected void loginFacebook() {
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(this).build();
Session.setActiveSession(session);
currentSession = session;
}
if (currentSession.isOpened()) {
//Toast("User Already Logged In");
} else if (!currentSession.isOpened()) {
OpenRequest op = new Session.OpenRequest(this);
op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
op.setCallback(statusCallback);
List<String> permissions = Arrays.asList("publish_stream", "email");
op.setPermissions(permissions);
Session session = new Builder(this).build();
Session.setActiveSession(session);
session.openForPublish(op);
}
}
}

将Android FacebookSDK添加到项目文件夹和configuration.json文件中

"header_paths": ["./FacebookSDK/src"],
"sources": [ "java-wrappers/BridgeKitExt.m" ],
"java_sources": [ "java/src/com/duksel/apportable/BridgeKitExt.java" ],
"java_sourcepaths": [ "java/src" , "FacebookSDK/src" ],

但是当构建时,得到这个错误

.../BridgeKitExt.java:128: class, interface, or enum expected
Session session = Session.getActiveSession();
^
/BridgeKitExt.java:129: class, interface, or enum expected
if (session != null && (session.isOpened() || session.isClosed())) {
^

您的BridgeKitExt.java文件中似乎只有一个语法错误。。。

在您发布的代码中很难匹配大括号,因为缩进似乎不正确,但在onActivityResult函数的末尾可能会有一个额外的大括号吗?

相关内容

  • 没有找到相关文章

最新更新