【发布时间】:2021-01-29 15:45:03
【问题描述】:
我想在 google api 的帮助下每天监控我的网站性能
我尝试从 pagespeedonline 的 googleapi 获取网络请求中的 items
但它不适用于我的代码
REST API 链接:
我试着变得特别
lighthouseResult -> audits -> network-requests ->details-> items
并将每个项目存储到记录中...
我试过下面的代码
package FirstTestNgPackage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.json.*;
public class testingJSON {
static String inline = "";
public static void main(String args[]) throws JSONException, InterruptedException, IOException {
// url
URL url = new URL("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://lifemachi.blogspot.com&key=AIzaSyBfHVlhNEnf26Ea8-ZOhiYOe0HrQZtLvRI&category=performance&strategy=desktop");
// read it from URL
Scanner sc = new Scanner(url.openStream()); Thread.sleep(300);
String jsonDataString = sc.nextLine();
while(sc.hasNext())
{
inline+=sc.nextLine();
}
sc.close();
List<String> list = new ArrayList<String>();
// just print that inline var
System.out.println(inline);
System.out.println("--------1");
}
}
我得到了正确的输出... 但是如何将项目值存储在列表中?
提前致谢
【问题讨论】:
-
不是您的问题的答案,但希望是您实际问题的答案(监控站点性能) 如果您想监控日常活动,PSI API 对您没有那么有用。您可能想查看this answer I gave,了解如何衡量应用程序的性能并构建您自己的解决方案,因为它将为您提供更好的真实世界数据,因为现场数据的 PSI 数据是 28 天滚动的,而实验室数据不会波动除非你改变东西
标签: java json google-api pagespeed-insights google-pagespeed-insights-api