PageSpeed API - 批处理 Java
PageSpeed API - Batching in Java
我在 Java 应用程序中使用 PageSpeed API。它工作正常,但由于我需要检查很多 URL,因此我需要进行一些批处理。我一直在网上和他们的文档中查找,但找不到任何有关如何执行此操作的信息。
有人知道吗?
我正在连接一个普通的 HTTP 请求,因为事实证明它比使用他们的包更快,但我假设对于批处理,他们的包可能更好,所以这是我在这两种情况下使用的代码:
HTTP 请求:
String call = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + SITE + "&key=" + GOOGLE_KEY + "&strategy=" + DEVICE;
URL url = new URL(call);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(response.toString());
PageSpeed Java 包:
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
httpRequest.setReadTimeout(60000); // 60 seconds
}
};
Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName("APP_NAME").build();
Pagespeedonline.Pagespeedapi.Runpagespeed runpagespeed = p.pagespeedapi().runpagespeed(SITE).setKey(GOOGLE_KEY).setStrategy(DEVICE);
PagespeedApiPagespeedResponseV5 response = runpagespeed.execute();
两者都工作正常,我只需要知道如何在两者中的任何一个中集成批处理。
谁能帮帮我?
非常感谢!
我做到了,但是忘记分享了,这里是:
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
httpRequest.setReadTimeout(60000); // 60 seconds
}
};
BatchRequest batch = p.batch(httpRequestInitializer);
Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName(APP_NAME).build();
//Loop to add as many as you want
for (int i = 0; i < urls.size(); ++i) {
Pagespeedonline.Pagespeedapi.Runpagespeed check = p.pagespeedapi().runpagespeed(urls.get(i)).setKey(GOOGLE_KEY).setStrategy(DEVICE);
check.queue(batch, callback);
}
batch.execute();
这是回调:
JsonBatchCallback<PagespeedApiPagespeedResponseV5> callback = new JsonBatchCallback<PagespeedApiPagespeedResponseV5>() {
public void onSuccess(PagespeedApiPagespeedResponseV5 response, HttpHeaders responseHeaders) {
//ON SUCCESS
}
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
//ON FAILURE
}
};
我在 Java 应用程序中使用 PageSpeed API。它工作正常,但由于我需要检查很多 URL,因此我需要进行一些批处理。我一直在网上和他们的文档中查找,但找不到任何有关如何执行此操作的信息。 有人知道吗?
我正在连接一个普通的 HTTP 请求,因为事实证明它比使用他们的包更快,但我假设对于批处理,他们的包可能更好,所以这是我在这两种情况下使用的代码:
HTTP 请求:
String call = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + SITE + "&key=" + GOOGLE_KEY + "&strategy=" + DEVICE;
URL url = new URL(call);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(response.toString());
PageSpeed Java 包:
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
httpRequest.setReadTimeout(60000); // 60 seconds
}
};
Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName("APP_NAME").build();
Pagespeedonline.Pagespeedapi.Runpagespeed runpagespeed = p.pagespeedapi().runpagespeed(SITE).setKey(GOOGLE_KEY).setStrategy(DEVICE);
PagespeedApiPagespeedResponseV5 response = runpagespeed.execute();
两者都工作正常,我只需要知道如何在两者中的任何一个中集成批处理。
谁能帮帮我?
非常感谢!
我做到了,但是忘记分享了,这里是:
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
httpRequest.setReadTimeout(60000); // 60 seconds
}
};
BatchRequest batch = p.batch(httpRequestInitializer);
Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName(APP_NAME).build();
//Loop to add as many as you want
for (int i = 0; i < urls.size(); ++i) {
Pagespeedonline.Pagespeedapi.Runpagespeed check = p.pagespeedapi().runpagespeed(urls.get(i)).setKey(GOOGLE_KEY).setStrategy(DEVICE);
check.queue(batch, callback);
}
batch.execute();
这是回调:
JsonBatchCallback<PagespeedApiPagespeedResponseV5> callback = new JsonBatchCallback<PagespeedApiPagespeedResponseV5>() {
public void onSuccess(PagespeedApiPagespeedResponseV5 response, HttpHeaders responseHeaders) {
//ON SUCCESS
}
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
//ON FAILURE
}
};