简体   繁体   English

Java HTTP Client输出空JSON

[英]Java HTTP Client outputs empty JSON

I am running the following code: 我正在运行以下代码:

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://10.0.0.22:8086/db/cadvisorDB/series?u=root&p=root&q=select%20max(memory_usage)%20from%20stats%20where%20container_name%20%3D%27execution_container_"+bench_list+"_"+i+"%27%20and%20memory_usage%20%3C%3E%200%20group%20by%20container_name");
//Thread.sleep(10000);
CloseableHttpResponse requestResponse = httpclient.execute(httpGet);
String response=EntityUtils.toString(requestResponse.getEntity());
System.out.println(response);

Output console: 输出控制台:

[]

When I wait for the HttpResponse 30s it works. 当我等待HttpResponse 30s时,它可以工作。 I got the complete response (JSON with data points) : 我得到了完整的响应(带数据点的JSON):

Thread.sleep(30000);

IS it possible using Apache Java client to tell the client to wait until getting a value different than "[]". 是否可以使用Apache Java客户端告诉客户端等到获得的值不同于“[]”。 I mean a non empty Json. 我的意思是一个非空的Json。

Using timeouts does not solve the problem. 使用超时并不能解决问题。

Thank you in advance 先感谢您

Then setting the timeout will work. 然后设置超时将起作用。

HttpGet request = new HttpGet(url);

    // set timeouts as you like
    RequestConfig config = RequestConfig.custom()
            .setSocketTimeout(60 * 1000).setConnectTimeout(20 * 1000)
            .setConnectionRequestTimeout(20 * 1000).build();

    request.setConfig(config);

To be specific, no it is not possible simply using HttpClient "to tell the client to wait until getting a value different than" what it gets when the call is over. 具体而言,不能简单地使用HttpClient“告诉客户端等到获得的值不同于”调用结束时获得的值。 You have to program this yourself (in a loop or something.) 你必须自己编程(循环或其他东西。)

  • Does it make a difference if the sleep() is before HttpClients.createDefault() ? 如果sleep() HttpClients.createDefault() 之前,它会有所不同吗?
  • Is it possible that your server at 10.0.0.22:8086 is just not ready when your code is executed? 您的代码执行时,10.0.0.22:8086的服务器是否可能尚未就绪? Is this server launched by the same app? 此服务器是否由同一个应用程序启动?

I had also same issue , but problem was 2 Http call making sequentially. 我也有同样的问题,但问题是顺序2 Http调用。 so i have putted Thread.sleep(2000) for seconds and it worked. 所以我已经推出Thread.sleep(2000)几秒钟而且它有效。

Please confirm if your code making two rest call sequentially? 请确认您的代码是否依次进行两次休息呼叫?

then may be you can place Thread.sleep just before second http call. 那么你可以在第二次http调用之前放置Thread.sleep。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM