com.github.kevinsawicki.http.HttpRequest.acceptJson()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(1.5k)|赞(0)|评价(0)|浏览(146)

本文整理了Java中com.github.kevinsawicki.http.HttpRequest.acceptJson()方法的一些代码示例,展示了HttpRequest.acceptJson()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.acceptJson()方法的具体详情如下:
包路径:com.github.kevinsawicki.http.HttpRequest
类名称:HttpRequest
方法名:acceptJson

HttpRequest.acceptJson介绍

[英]Set the 'Accept' header to 'application/json'
[中]将“Accept”标题设置为“application/json”

代码示例

代码示例来源:origin: com.codeslap/github-jobs-java-api

String response = HttpRequest.get(url).acceptGzipEncoding().acceptJson().body();
if (response == null) {
  throw new RuntimeException("Error calling API; it returned null.");

代码示例来源:origin: MrPowerGamerBR/TemmieWebhook

public void run() {
    String strResponse = HttpRequest.post(url)
        .acceptJson()
        .contentType("application/json")
        .header("User-Agent", "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11") // Why? Because discordapp.com blocks the default User Agent
        .send(gson.toJson(dm))
        .body();
    if (!strResponse.isEmpty()) {
      Response response = gson.fromJson(strResponse, Response.class);
      try {
        if (response.getMessage().equals("You are being rate limited.")) {
          // If we are rate limited, let's wait a few seconds before sending
          // the message again.
          try {
            // TODO: Should we really block the main thread just to send the message?
            // Blocking the main thread isn't a good idea... at all...
            Thread.sleep(response.getRetryAfter());
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          sendMessage(dm);
        } else {
          throw new WebhookException(response.getMessage());
        }
      } catch (Exception e) {
        throw new WebhookException(strResponse);
      }
    }
  }
};

相关文章

微信公众号

最新文章

更多

HttpRequest类方法