我从初始 URI GET 请求开始。我得到返回结果以及指向下一批记录的链接。我循环遍历直到检索到所有记录。似乎性能受到了影响,因为记录器显示 HttpClientHelper 在每次请求后启动和停止。这是可以避免的吗?
Client client = new Client(new Context(), Protocol.HTTP);
ClientResource clientResource;
JsonRepresentation rep;
JSONObject object;
while(nextURI !=null)
{
clientResource = new ClientResource(nextURI);
//excessive HttpClientHelper start/stop triggered at next line
rep = new JsonRepresentation(service.get(MediaType.APPLICATION_JSON));
clientResource.setNext(client);
.
.
.
}
记录器显示以下内容:
May 13, 2015 9:30:52 AM org.restlet.ext.net.HttpClientHelper start
INFO: Starting the HTTP client May 13, 2015 9:30:53 AM org.restlet.ext.net.HttpClientHelper start
INFO: Starting the HTTP client May 13, 2015 9:30:53 AM org.restlet.ext.net.HttpClientHelper start
INFO: Starting the HTTP client May 13, 2015 9:30:53 AM org.restlet.ext.net.HttpClientHelper stop
INFO: Stopping the HTTP client May 13, 2015 9:30:53 AM org.restlet.ext.net.HttpClientHelper stop
INFO: Stopping the HTTP client
最佳答案
我认为您调用方法 setNext
太迟了。您需要在任何 HTTP 调用之前以及在创建 ClientResource
实例之后立即调用它,以防止客户端连接器在每次调用时启动。
您可以在下面找到示例:
Client client = new Client(new Context(), Protocol.HTTP);
for (int i = 0; i < 5; i++) {
ClientResource clientResource = new ClientResource("http://...");
clientResource.setNext(client);
Representation representation = clientResource.get();
(...)
}
在这种情况下,客户端连接器只启动一次。
希望对你有帮助, 蒂埃里
关于java - reSTLet 分页 HttpClientHelper 启动/停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30218116/