Android Facebook喜欢发布

时间:2015-05-07 10:32:51

标签: android facebook facebook-graph-api youtube facebook-like

我正在使用facebook集成创建应用。我关注Facebook开发者'安装和配置SDK的指南。我还有一个工作的Facebook登录按钮。 现在,下一步是从公共页面显示每个youtube帖子,并使用户可以喜欢它。

所以,我创建了一个GraphRequest并解析了JSON对象的返回。我检索所有youtube视频名称以及JSON对象的链接" actions"对于"喜欢"提起。该链接将由" setObjectIdAndType()"设置。这是LikeView类的一种方法。但是当我登录并点击按钮时,它显示按钮被点击并被喜欢,但在Facebook页面上没有我的喜欢!

这是我的片段页面:

public class FacebookLikes extends Fragment{

CallbackManager callbackManager;
ProfileTracker profileTracker;
AccessTokenTracker accessTokenTracker;
TextView textUser;
ListView listView;
CheckConnectivity checkConnectivity;
LoginButton loginButton;

VideoList videoList;

AccessToken currentToken;
Profile currentLogInProfile;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

    checkConnectivity = new CheckConnectivity(getActivity());

    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(
                AccessToken oldAccessToken,
                AccessToken currentAccessToken) {
            currentToken = currentAccessToken;
        }
    };

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(
                Profile oldProfile,
                Profile currentProfile) {
            currentLogInProfile = currentProfile;
            if(currentLogInProfile != null) {
                textUser.setText(getActivity().getResources().getString(R.string.text_hello) + " " +
                currentLogInProfile.getName());
            }else{
                textUser.setText("Non sei loggato");
            }
        }
    };
}

@Override
public void onPause() {
    super.onPause();
    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(getActivity().getApplicationContext());
}

@Override
public void onResume() {
    super.onResume();
    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(getActivity().getApplicationContext());
}

@Override
public void onDestroy() {
    super.onDestroy();
    profileTracker.stopTracking();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.facebook_fragment, container, false);
    loginButton = (LoginButton)v.findViewById(R.id.login_button);

    loginButton.setFragment(this);
    loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));

    listView = (ListView)v.findViewById(R.id.listViewVideos);

    videoList = new VideoList(getActivity(), R.layout.fb_list_item, new ArrayList<VideoItem>());
    videoList.setFragment(this);
    listView.setAdapter(videoList);

    textUser = (TextView)v.findViewById(R.id.txtUserLogeedIn);

    currentToken = AccessToken.getCurrentAccessToken();
    currentLogInProfile = Profile.getCurrentProfile();
    if(currentLogInProfile != null) {
        textUser.setText(getActivity().getResources().getString(R.string.text_hello) + " " +
                currentLogInProfile.getName());
    }else{
        textUser.setText("Non sei loggato");
    }

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {

        }
    });

    Bundle parameters = new Bundle();
    parameters.putString("fields", "posts.since(2015-04-02){name,caption,actions}");
    graphRequest.setParameters(parameters);
    if(checkConnectivity.isConnected()) {
        graphRequest.executeAsync();
    }
    return v;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
}

这里是图表请求:

 GraphRequest graphRequest = GraphRequest.newGraphPathRequest(
            AccessToken.getCurrentAccessToken(),
            "/acfactornola",
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                    Log.d("JSON", response.toString());
                    JSONObject object = response.getJSONObject();
                    try {
                        if(object != null && object.has("posts")) {
                            JSONObject post = object.getJSONObject("posts");
                            if(post != null && post.has("data")) {
                                JSONArray data = post.getJSONArray("data");
                                for (int j = 0; j < data.length(); j++) {
                                    JSONObject current = data.getJSONObject(j);
                                    if (current != null && current.has("caption")) {
                                        String caption = (String) current.get("caption");
                                        if (caption != null && caption.compareTo("youtube.com") == 0) {
                                            JSONArray actions = current.getJSONArray("actions");
                                            JSONObject linkObject = actions.getJSONObject(1);
                                            videoList.add(new VideoItem(current.getString("name"), linkObject.getString("link")));
                                            videoList.notifyDataSetChanged();
                                            Log.d("JSON", "Name: " + current.getString("name"));
                                            Log.d("JSON", "Link: " + linkObject.getString("link"));
                                        }
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

每个Facebook视频都在数组适配器中......以下是代码:

public class VideoList extends ArrayAdapter<VideoItem>{

private int resource;
private LayoutInflater inflater;
private Fragment fragment;

public VideoList(Context context, int resource, List<VideoItem> objects) {
    super(context, resource, objects);
    this.inflater = LayoutInflater.from(context);
    this.resource = resource;
}

public void setFragment(Fragment fragment) {
    this.fragment = fragment;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    VideoItem videoItem = getItem(position);

    ViewHolder holder;

    if(convertView == null){
        convertView = inflater.inflate(resource, parent, false);
        holder = new ViewHolder();
        holder.textVideoName = (TextView)convertView.findViewById(R.id.textVideo);
        holder.likeView = (LikeView)convertView.findViewById(R.id.likeButton);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder)convertView.getTag();
    }

    holder.textVideoName.setText(videoItem.getVideoName());

    holder.likeView.setLikeViewStyle(LikeView.Style.BUTTON);
    holder.likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);
    holder.likeView.setHorizontalAlignment(LikeView.HorizontalAlignment.LEFT);
    holder.likeView.setObjectIdAndType(videoItem.getLikeLink(), LikeView.ObjectType.PAGE);
    holder.likeView.setFragment(fragment);

    return convertView;
}

private static class ViewHolder{
    TextView textVideoName;
    LikeView likeView;
}
}

在清单中我认为插入这些行是正确的:

<activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationName"
        android:value="@string/app_name" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>
    <provider
        android:authorities="com.facebook.app.FacebookContentProviderAPPID"
        android:name="com.facebook.FacebookContentProvider"
        android:exported="true"/>

我的APPID是正确的APPID。 在Facebook开发者控制台上,我为debug.keystore注册了HASHKEY。

点击它时,为什么Like功能不起作用?任何输入都将受到赞赏。

0 个答案:

没有答案