1

我正在开发一个 Android 健身应用程序,我正在尝试使用 Open Graph API 在 Facebook 上发布活动数据点位置。我使用了以下代码,它只发布了location1,但没有添加到列表中的其他位置。

public void shareWorkout (View v){
    if (FacebookDialog.canPresentShareDialog(getApplicationContext(), FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
        OpenGraphObject course = OpenGraphObject.Factory.createForPost(null);
        course.setType("fitness.course");
        course.setTitle("Walking Course");
        course.setUrl("http://samples.ogp.me/136756249803614");
        course.setDescription("walked a course");

        GraphObject location1 = GraphObject.Factory.create();
        location1.setProperty("latitude", 31.90592568833381f);
        location1.setProperty("longitude", 35.20052916370332f);
        location1.setProperty("altitude", 0.0f);

        GraphObject location2 = GraphObject.Factory.create();
        location2.setProperty("latitude", 31.912331976927817f);
        location2.setProperty("longitude", 35.205821581184864f);
        location2.setProperty("altitude", 0.0f);

        List<GraphObject> locations = new ArrayList<GraphObject>();
        locations.add(location1);
        locations.add(location2);

        GraphObject metrics = GraphObject.Factory.create();
        metrics.setProperty("location", locations);

        course.getData().setProperty("metrics", metrics);

        OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
        action.setProperty("course", course);

        FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(this,
        action, "fitness.walks", "course").build();            
        uiHelper.trackPendingDialogCall(shareDialog.present());
    }
}
4

2 回答 2

4

我也尝试在 facebook 上分享一门课程。

就像提到的facebook 提交标准一样,您应该包括尽可能多的适当指标(速度、地理点、距离)。

因此,对于每个新位置,您必须创建一个新的度量对象,包括位置、时间戳、距离和步速,并将这个度量数组添加到度量属性。

这是我的工作示例:

private void shareWorkout(Location[] locations) {
    NumberFormat doubleFormat = NumberFormat.getInstance(Locale.US);
    doubleFormat.setMaximumFractionDigits(6);
    doubleFormat.setMaximumIntegerDigits(3);
    doubleFormat.setGroupingUsed(false);

    OpenGraphObject course = OpenGraphObject.Factory
            .createForPost("fitness.course");
    course.setTitle("Sample Course");
    List<GraphObject> metrics = new ArrayList<GraphObject>();
    Location prevLocation = null;

    for (Location location : locations) {
        GraphObject metric = GraphObject.Factory.create();
        double distance = calcDistance(prevLocation, location);
        double speed = location.getSpeed() * LocationUtils.MS_TO_KMH;
        double pace = speed == 0 ? 0.0 : 60.0 / speed;
        metric.setProperty("location:longitude",
                doubleFormat.format(location.getLongitude()));
        metric.setProperty("location:latitude",
                doubleFormat.format(location.getLatitude()));
        metric.setProperty("location:altitude",
                doubleFormat.format(location.getAltitude()));
        metric.setProperty("timestamp",
                StringUtils.formatDateTimeIso8601(location.getTime()));
        metric.setProperty("distance:value", doubleFormat.format(distance));
        metric.setProperty("distance:units", "km");
        metric.setProperty("pace:value", doubleFormat.format(pace));
        metric.setProperty("pace:units", "s/m");
        metrics.add(metric);
        prevLocation = location;
    }
    GraphObject courseData = course.getData();
    courseData.setProperty("metrics", metrics);
    courseData.setProperty("distance:value", calcTime(locations));
    courseData.setProperty("distance:units", "min");
    courseData.setProperty("distance:value",
            doubleFormat.format(calcTotalDistance(locations)));
    courseData.setProperty("distance:units", "km");
    OpenGraphAction action = GraphObject.Factory
            .create(OpenGraphAction.class);
    action.setProperty("course", course);
    action.setType("fitness.walks");

    RequestBatch requestBatch = new RequestBatch();
    Request request = Request.newPostOpenGraphActionRequest(
            Session.getActiveSession(), action, shareCallback);
    requestBatch.add(request);
    requestBatch.executeAsync();
}
于 2014-10-07T15:35:05.323 回答
2

从 Facebook SDK 3.x 更新到 4.1 时,我遇到了类似的问题。

在我的情况下,我已经在目标 Web url 上拥有了所有健身元数据(使用参考https://developers.facebook.com/docs/opengraph/guides/fitness创建),问题是关于共享的文档(https: //developers.facebook.com/docs/sharing/android#advanced)有点误导。经过几次尝试,我发现它fitness:course也可以作为 URL 传递,而不仅仅是ShareGraphObject

ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
            .setActionType("fitness.runs")
            .putString("fitness:course", url)
            .build();

ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
            .setPreviewPropertyName("fitness:course")
            .setAction(action)
            .build();

比打电话

if (shareDialog.canShow(ShareOpenGraphContent.class)) {
    shareDialog.show(content);
}

希望这可以帮助!

于 2015-07-11T20:51:39.353 回答