我在Java中使用此代码连接到API并检索一些数据。连接成功但我不知道如何打印响应,如PHP的print_r
,并在JSON中看到响应。
Java代码:
public static void main(String[] args) throws IOException, UnirestException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://sample.talentlms.com/api/v1/users/")
.get()
.addHeader("authorization", "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX==")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
.build();
Response response = client.newCall(request).execute();
System.out.print(response);
}
输出:
Response{protocol=http/1.1, code=200, message=OK, url=https://sample.talentlms.com/api/v1/users/}
答案 0 :(得分:0)
对于使用OkHTTP进行HTTP请求的任何其他人。只需在打印中执行以下操作。
System.out.print(response.body().string());