使用ShareOpenGraphObject

时间:2016-09-01 13:32:56

标签: android facebook

我正在创建一个需要使用sharing view and open graph stories将运行发布到Facebook的应用程序。 它会发布时间和距离等基本信息,但由于某种原因,我无法在地图上显示课程,例如:

enter image description here

这是我必须创建帖子的代码:

    for (int i = 0; i < dataPoints.size(); i++) {
        DataPoint dataPoint = dataPoints.get(i);
        ShareOpenGraphObject.Builder metric = new ShareOpenGraphObject.Builder();
        metric.putDouble("fitness:metrics:location:latitude", dataPoint.latitude);
        metric.putDouble("fitness:metrics:location:longitude", dataPoint.longitude);
        metric.putDouble("fitness:metrics:location:altitude", dataPoint.altitude);
        locations.add(metric.build());
    }

    ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
            .putString("fb:app_id", parent.getString(R.string.facebook_app_id))
            .putString("og:type", "fitness.course")
            .putString("og:title", "Workout on " + date)
            .putString("og:url", BASE_URL + getApp().getUserName() + "/activity/" + summary.Id)
            .putInt("fitness:duration:value", summary.TijdInSecondes)
            .putString("fitness:duration:units", "s")
            .putDouble("fitness:distance:value", samenvatting.AfstandInMeters / 1000f)
            .putString("fitness:distance:units", "km")
            .putInt("fitness:speed:value", samenvatting.AfstandInMeters / samenvatting.TimeInSeconds)
            .putString("fitness:speed:units", "m/s")
            .putInt("fitness:calories", samenvatting.Kcal)
            .putObjectArrayList("fitness:metrics:location", locations)
            .build();

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

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

我还尝试将指标编码为包含数组索引,如this bug report所述:

        for (int i = 0; i < dataPoints.size(); i++) {
        DataPoint dataPoint = dataPoints.get(i);
        ShareOpenGraphObject.Builder metric = new ShareOpenGraphObject.Builder();
        metric.putDouble("fitness:metrics[" + i + "]:location:latitude", dataPoint.latitude);
        metric.putDouble("fitness:metrics[" + i + "]:location:longitude", dataPoint.longitude);
        metric.putDouble("fitness:metrics[" + i + "]:location:altitude", dataPoint.altitude);
        locations.add(metric.build());
    }

但那也没有解决它。我错过了什么?

1 个答案:

答案 0 :(得分:1)

事实证明它比预期的要简单得多。而不是为位置创建单独的数组,只需将它们直接添加到原始的ShareOpenGraphObject,但手动编码数组编号。

        ShareOpenGraphObject.Builder object = new ShareOpenGraphObject.Builder()
            .putString("fb:app_id", parent.getString(R.string.facebook_app_id))
            .putString("og:type", "fitness.course")
            .putString("og:title", "Workout on " + date)
            .putString("og:url", BASE_URL + getApp().getUserName() + "/activity/" + samenvatting.Id)
            .putInt("fitness:duration:value", samenvatting.TijdInSecondes)
            .putString("fitness:duration:units", "s")
            .putDouble("fitness:distance:value", samenvatting.AfstandInMeters / 1000f)
            .putString("fitness:distance:units", getApp().getSettings().speedUnitIsMetric() ? "km" : "mi")
            .putInt("fitness:speed:value", samenvatting.AfstandInMeters / samenvatting.TijdInSecondes)
            .putString("fitness:speed:units", "m/s")
            .putInt("fitness:calories", samenvatting.Kcal);


    for (int i = 0; i < dataPoints.size(); i++) {
        DataPoint dataPoint = dataPoints.get(i);
        object.putDouble("fitness:metrics[" + i + "]:location:latitude", dataPoint.latitude);
        object.putDouble("fitness:metrics[" + i + "]:location:longitude", dataPoint.longitude);
        object.putDouble("fitness:metrics[" + i + "]:location:altitude", dataPoint.altitude);
    }