为什么快速api不返回json?

时间:2020-04-19 13:19:43

标签: java json

我是网络爬虫的初学者,目前正在使用快速api平台开发应用程序以获取一些json。寻找特定端点后,系统会提供一个代码段,您应将其粘贴到程序中。因此,我运行了以下代码:

import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AAA {

    public static void main(String args[]) throws IOException {

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("https://api-football-v1.p.rapidapi.com/v2/fixtures/league/1383/last/10?timezone=Europe%252FLondon")
                .get()
                .addHeader("x-rapidapi-host", "api-football-v1.p.rapidapi.com")
                .addHeader("x-rapidapi-key", "myRapidAPIKey")
                .build();

        Response response = client.newCall(request).execute();
        System.out.println(response.toString());
    }
}

但是无论我选择哪个端点,都反复显示相同的 NOT JSON 输出:

Response{protocol=http/1.1, code=200, message=OK, url=https://api-football-v1.p.rapidapi.com/v2/predictions/157462}

我不使用Maven。我刚刚包含了必要的jar,以便我的代码正常工作。我究竟做错了什么?预先谢谢你?

1 个答案:

答案 0 :(得分:0)

json通常包含在响应主体中。所以你必须尝试类似的东西:

System.out.println(response.body().string());

代替

System.out.println(response.toString());