解析列表>在安卓中?

发布于 2024-12-25 13:36:22 字数 1486 浏览 2 评论 0 原文

我正在使用 Google Java 客户端库< 开发自定义 Adsense 报告工具/a> 适用于Android。我已成功通过身份验证,并且可以对服务器进行 API 调用。但现在当我收到响应时,我不知道如何解析它并向用户正确显示结果。

根据 javaDocs,AdsenseReportsGenerateResponse.getRows() 生成一个 List>但我有点迷失了如何正确解析它来获取:

-Today's earnings
-Yesterday's earnings
-Last 7 days
-Last month
-From the beginning of time

这是与问题

        Reports.Generate request = adsense.reports().generate(startDate, endDate);    

        request.setMetric(Arrays.asList("PAGE_VIEWS", "AD_REQUESTS", "AD_REQUESTS_COVERAGE", "CLICKS",
                "AD_REQUESTS_CTR", "COST_PER_CLICK", "AD_REQUESTS_RPM", "EARNINGS"));

        request.setDimension(Arrays.asList("DATE", "WEEK", "MONTH"));    
        request.setSort(Arrays.asList("+DATE"));

        AdsenseReportsGenerateResponse response = request.execute();

        //TODO: Here be dragons
        response.getRows();

编辑相关的我的代码的一部分:这是提到 getRow()

嗯,这个网站上似乎没有人可以提供帮助?!

I'm developing a custom Adsense report tool using Google Java Client Library for Android. I've successfully authenticated and can make API calls to the server. but now when I receive the response, I don't know how to parse it and correctly show the result to user.

According to the javaDocs, AdsenseReportsGenerateResponse.getRows() generates a List> But I'm kinda lost how to properly parse it to get:

-Today's earnings
-Yesterday's earnings
-Last 7 days
-Last month
-From the beginning of time

Here's part of my code related to the question

        Reports.Generate request = adsense.reports().generate(startDate, endDate);    

        request.setMetric(Arrays.asList("PAGE_VIEWS", "AD_REQUESTS", "AD_REQUESTS_COVERAGE", "CLICKS",
                "AD_REQUESTS_CTR", "COST_PER_CLICK", "AD_REQUESTS_RPM", "EARNINGS"));

        request.setDimension(Arrays.asList("DATE", "WEEK", "MONTH"));    
        request.setSort(Arrays.asList("+DATE"));

        AdsenseReportsGenerateResponse response = request.execute();

        //TODO: Here be dragons
        response.getRows();

Edit: Here is the javaDoc which mentions the getRow()

Hmm it seems nobody on this site can help?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总攻大人 2025-01-01 13:36:22

您应该会发现我们的示例代码很有用:http://code .google.com/p/google-api-java-client/wiki/APIs#AdSense_Management_API

也就是说,这是您感兴趣的文件:http://code.google.com/p/google-api-java-client/source/browse/adsense-cmdline-sample/src/main/java/com/google/api/services/ Samples/adsense/cmdline/GenerateReport.java?repo=samples

以下是用于打印输出的代码片段。请注意,这是针对命令行应用程序的,但应该很容易适应:

if ((response.getRows() != null) && !response.getRows().isEmpty()) {
  // Display headers.
  for (AdsenseReportsGenerateResponseHeaders header : response.getHeaders()) {
    System.out.printf("%25s", header.getName());
  }
  System.out.println();

  // Display results.
  for (List<String> row : response.getRows()) {
    for (String column : row) {
      System.out.printf("%25s", column);
    }
    System.out.println();
    }

  System.out.println();
} else {
  System.out.println("No rows returned.");
}

至于获取不同时间段的数据,您可能应该运行不同的报告,而不是将其全部塞入一个报告中,因为这将需要不同的开始日期和结束日期。其工作原理如下:

  • 今天的收入:将开始和结束日期设置为今天,将维度列表设置为 DATE
  • 昨天的收入:将开始和结束日期设置为昨天,将维度列表设置为 DATE
  • 最近 7 天:如果您愿意每天的数据,然后将开始日期设置为 7 天前,结束日期设置为今天,并将维度列表设置为 DATE。如果您想汇总统计数据,您可能需要自己计算,因为“周”和“月”指的是日历周和月,而不是最近 7 天。
  • 上个月:开始日期为上个月 1 日,结束日期为该月最后一天,维度 MONTH。
  • All time:您希望如何汇总?每月?然后将开始日期设置为 1980 年 1 月 1 日,结束日期设置为今天,维度设置为月份。

这篇博文应该有助于更好地理解报告概念: http://adsenseapi.blogspot.com/2011/11/adsense-management-api-diving-into.html

如果您需要其他帮助,请告诉我!

You should find our sample code useful: http://code.google.com/p/google-api-java-client/wiki/APIs#AdSense_Management_API

Namely, this is the file you're interested in: http://code.google.com/p/google-api-java-client/source/browse/adsense-cmdline-sample/src/main/java/com/google/api/services/samples/adsense/cmdline/GenerateReport.java?repo=samples

Here's a snippet of code to print the output. Mind you, this is for a command line application, but should be easily adaptable:

if ((response.getRows() != null) && !response.getRows().isEmpty()) {
  // Display headers.
  for (AdsenseReportsGenerateResponseHeaders header : response.getHeaders()) {
    System.out.printf("%25s", header.getName());
  }
  System.out.println();

  // Display results.
  for (List<String> row : response.getRows()) {
    for (String column : row) {
      System.out.printf("%25s", column);
    }
    System.out.println();
    }

  System.out.println();
} else {
  System.out.println("No rows returned.");
}

As for getting the data for different periods of time, you should probably be running different reports, not cramming it all into one, as that would take different start dates and end dates. Here's how it works:

  • Today's earnings: set the start and end dates to today, set the dimension list to just DATE
  • Yesterday's earnings: set the start and end date to yesterday, set the dimension list to just DATE
  • Last 7 days: if you want data per day, then you set the start date to 7 days ago, the end date to today, and the dimension list to just DATE. If you want to aggregate the stats, you may need to calculate this yourself, as WEEK and MONTH refer to a calendar week and month, not the last 7 days.
  • Last month: start date 1st of last month, end date last day of the month, dimension MONTH.
  • All time: how do you want this aggregated? Per month? Then set the start date to, say, 1980-1-1, end date to today and dimension to MONTH.

This blog post should help with understanding reporting concepts a bit better: http://adsenseapi.blogspot.com/2011/11/adsense-management-api-diving-into.html

Let me know if you need help with anything else!

撩动你心 2025-01-01 13:36:22

据我了解 API,它不是一个 List 。试试这个:

String[][] array = response.getRows();

for (int i = 0; i < array.getSize(); i++){

    String dimension = array[i][0];
    String metric = array[i][1];

    //Do what you want with them

}

据我所知,我写这篇文章是因为 API 说它有一个维度列表,其中一个值用于字符串,一个值用于指标。

如果您期望每行有几个单元格(我相信 API 不能以这种方式工作),您需要在内部添加另一个单元格,并可能使用诸如 array[i].getSize 之类的方法获取当前列表的大小()

如果对您没有帮助,请回帖。

编辑:我现在明白了。试试这个:

List list = response.getRows();

for (int i = 0; i < list.size(); i++){

List<String> list2 = list.get(i);

for (int j = 0; j < list2.size(); j++){
String value = list2.get(j);
//Do what you want
}

}

Its not a List<List> as far as I understand the api. Try this:

String[][] array = response.getRows();

for (int i = 0; i < array.getSize(); i++){

    String dimension = array[i][0];
    String metric = array[i][1];

    //Do what you want with them

}

I am writing this because the API says it has a list of dimensions with one value for the string and one for the metric, as far as I understand.

If you expect several cells on each row (Which I believe the API doesn't work that way), you need to add another for inside and get the size of the current list probably with something like array[i].getSize()

Post back if it doesn't help you.

Edit: I see now. Try this:

List list = response.getRows();

for (int i = 0; i < list.size(); i++){

List<String> list2 = list.get(i);

for (int j = 0; j < list2.size(); j++){
String value = list2.get(j);
//Do what you want
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文