[英]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
. 我正在一个需要我使用Anilist api v2的项目上工作,该版本使用
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. 我已经尝试了几天,甚至连我大学的2位讲师都没有帮助,他们以前都没有使用过graphql。
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. 在下面发布的代码中,我使用了来自另一个stackoverflow问题的
HTTP POST
代码,由于某种原因,我不断收到响应代码400
,这向我提示了某种语法错误。 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. 我已经尝试了多种格式的代码,但是我还没有偶然找到合适的格式,而且由于我对编程还很陌生,所以我无法真正理解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 致未来的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 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忽略了过多的空白和换行符,但我想不是吗?
json = json.replace("\\n", " ").replace(" ", " ");
does the job admirably. 出色地完成了这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.