简体   繁体   中英

Anilist api v2 GRAPHQL

Thanks for any help in advanced.
Im working on aa project that requires me to use the Anilist api v2, which uses graphQL . I've tried and tried for a few days now, and even approached 2 lecturers in my university for help to no avail, none of them have worked with graphql before.

Code posted below, I used the HTTP POST code from another stackoverflow question, and for some reason I keep getting response code 400 , which suggests to me some kind of syntax error. I've tried a bunch of formats for the code, but I haven't yet stumbled upon the right one, and since i'm pretty new to programming, I can't really understand the examples on github.

Any help is appreciated, Thanks.

    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());
    }
}

To Future Googlers

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 example:

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

This works well for me, in my question, I didn't properly format the json I think, I'd heard that json ignores excessive whitespace and newlines, but I guess not? json = json.replace("\\n", " ").replace(" ", " "); does the job admirably.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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