繁体   English   中英

Anilist API v2 GRAPHQL

[英]Anilist api v2 GRAPHQL

感谢您的高级帮助。
我正在一个需要我使用Anilist api v2的项目上工作,该版本使用graphQL 我已经尝试了几天,甚至连我大学的2位讲师都没有帮助,他们以前都没有使用过graphql。

在下面发布的代码中,我使用了来自另一个stackoverflow问题的HTTP POST代码,由于某种原因,我不断收到响应代码400 ,这向我提示了某种语法错误。 我已经尝试了多种格式的代码,但是我还没有偶然找到合适的格式,而且由于我对编程还很陌生,所以我无法真正理解github上的示例。

任何帮助表示赞赏,谢谢。

    private static void aniList() {
    try {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://graphql.anilist.co");
        StringEntity entity = new StringEntity("\"query\": \"query { \n" +
                "  Media (id: 1, type: ANIME) { \n" +
                "    title {\n" +
                "      english\n" +
                "    }\n" +
                "  }\n" +
                "}\", " +
                "\"variables\": \"{}\"");
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.addHeader("Content-type", "application/json");

        CloseableHttpResponse response = client.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        assert (statusCode == 200) : "response status code = " + statusCode + ", it's meant to be 200";
        System.out.println("statusCode = " + statusCode);
        client.close();
        System.out.println(response.toString());
    } catch (Exception exp) {
        System.out.println("exception TRIGGERED");
        System.out.println(exp.getMessage());
    }
}

致未来的Google员工

try {
        String json = "{\"query\":\"";
        json += query;
        json += "\"}";

        json = json.replace("\n", " ").replace("  ", " ");
        URL url = new URL(endpoint);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        conn.addRequestProperty("Accept", "application/json");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");

        OutputStream os = conn.getOutputStream();
        os.write(json.getBytes("UTF-8"));
        os.close();

        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");

        in.close();
        conn.disconnect();
        return result;
    } catch (Exception exp) {
        System.out.println("exception TRIGGERED");
        System.out.println(exp.getLocalizedMessage());
        return "";
    }

查询示例:

query {
Media (search: "naruto", type: ANIME) {
id
title {
  english
  romaji
  native
}
  }
}

这对我来说效果很好,在我的问题中,我没有正确格式化我认为的json,听说json忽略了过多的空白和换行符,但我想不是吗? json = json.replace("\\n", " ").replace(" ", " "); 出色地完成了这项工作。

暂无
暂无

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

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