Anilist api v2 GRAPHQL
Posted
技术标签:
【中文标题】Anilist api v2 GRAPHQL【英文标题】: 【发布时间】:2019-01-26 05:36:30 【问题描述】:感谢您在高级方面的任何帮助。
我正在做一个需要我使用 Anilist api v2 的项目,它使用 graphQL
。我已经尝试了几天,甚至向我大学的 2 位讲师寻求帮助都无济于事,他们之前都没有使用过 graphql。
下面发布的代码,我使用了另一个 *** 问题中的 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());
【问题讨论】:
哦,我忘了,Anilist api docs anilist.gitbook.io/anilist-apiv2-docs/overview/graphql/… 我使用了 chrome 扩展“Altair”来测试我的查询 【参考方案1】:致未来的 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(" ", " ");
做得很好。
【讨论】:
以上是关于Anilist api v2 GRAPHQL的主要内容,如果未能解决你的问题,请参考以下文章
获取 r_liteprofile 和 r_emailaddress - api.linkedin.com/v2