如何使用Facebook见解请求故障键

时间:2018-06-27 07:36:25

标签: java facebook facebook-graph-api facebook-insights

我如何更改代码才能获得包含细分“性别”的响应。

让我们看下面的例子。我想使用性别作为“细分”来显示以下数据:

enter image description here

这是代码段,我使用:

   try {

        APIContext context = new APIContext("yourAccessToken", "yourAppSecret");
        AdAccount adAccount = new AdAccount("yourAdAccountId", context);

        // parameters
        Map<String, Object> parameterMap = new HashMap();
        parameterMap.put("time_increment", "1");
        parameterMap.put("date_preset", AdsInsights.EnumDatePreset.VALUE_YESTERDAY);

        // metric fields
        List<String> metricFields = new ArrayList<>();
        metricFields.add("impressions");
        metricFields.add("campaign_id");

        // breakdowns
        List<AdsInsights.EnumBreakdowns> breakdowns = new ArrayList<>();
        breakdowns.add(AdsInsights.EnumBreakdowns.VALUE_GENDER);

        // batch request
        BatchRequest batchRequest = new BatchRequest(context);

        for (Campaign campaign : adAccount.getCampaigns().requestAllFields().execute()) {

            APIRequest<AdsInsights> insights = new Campaign.APIRequestGetInsights(campaign.getId(), campaign.getContext()).requestAllFields();

            // setting breakdowns to be requested
            if (breakdowns != null) {
                parameterMap.put("breakdowns", breakdowns);
            }

            // setting metrics to be requested
            if (metricFields != null) {
                insights.requestFields(metricFields);
            }

            // setting Parameters to be requested
            if (parameterMap != null) {
                insights.setParams(parameterMap);
            }

            // add batch request
            insights.addToBatch(batchRequest);
        }

        List<APIResponse> insightsResult = batchRequest.execute();

    } catch (Exception e) {
        e.printStackTrace();
    }

这是我得到的答复:

{
   "campaign_id":"xxxxxxxx",
   "date_start":"2018-06-26",
   "date_stop":"2018-06-26",
   "impressions":"858"
}

这是我期望的答复:

{
   "campaign_id":"xxxxxxxx",
   "date_start":"2018-06-26",
   "date_stop":"2018-06-26",
   "impressions":"858",
   "gender":"Female"
}

我的代码有什么问题?

0 个答案:

没有答案