首页 > 解决方案 > 为什么快速 api 没有返回 json?

问题描述

我是 webscraping 的初学者,我目前正在开发一个使用快速 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。我刚刚包含了我的代码工作所需的罐子。我究竟做错了什么?先感谢您?

标签: javajson

解决方案


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

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

代替

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

推荐阅读