如何使用Java下载tvdb JSON数据



我有适当的API密钥和订户Pin,但我不确定如何创建命令行登录或从tvdb数据库中提取实际数据。有人能举个例子吗?

这是我正在使用的代码,但是它给了我一个405响应代码。

URL url = new URL("https://api4.thetvdb.com/v4/login?apikey=xxxxxxxx&pin=XXXXXXXX");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("accept", "application/json");
conn.setRequestMethod("GET");
conn.connect();
//Getting the response code
int responsecode = conn.getResponseCode();
System.out.println("ResponseCode: " + responsecode);
if (responsecode != 200) {
throw new RuntimeException("HttpResponseCode: " + responsecode);
} else {
String inline = "";
Scanner scanner = new Scanner(url.openStream());
//Write all the JSON data into a string using a scanner
while (scanner.hasNext()) {
inline += scanner.nextLine();
}
System.out.println("inline: " + inline);
}

这部分基本正确:

URL url = new URL("https://api4.thetvdb.com/v4/login?apikey=xxxxxxxx&pin=XXXXXXXX");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("accept", "application/json");
conn.setRequestMethod("GET");
conn.connect();
//Getting the response code
int responsecode = conn.getResponseCode();
System.out.println("ResponseCode: " + responsecode);
if (responsecode != 200) {
throw new RuntimeException("HttpResponseCode: " + responsecode);

…除了,根据文档,您需要将密钥和PIN作为请求主体中的JSON值传递,而不是作为URL查询参数。文档中有如下示例请求正文:

{
"apikey": "string",
"pin": "string"
}

所以,把这些从你的URL中删除:

URL url = new URL("https://api4.thetvdb.com/v4/login");
并将它们作为JSON对象发送。(我使用的是Java EE的javax。json包,但有几个其他的json库可以工作。)
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
// According to the documentation, /login is a POST call.
conn.setRequestMethod("POST");
conn.setDoOutput(true);
JsonObjectBuilder builder = Json.createObjectBuilder();
JsonObject requestBody =
builder.add("apikey", myAPIKey).add("pin", myPIN).build();
try (JsonWriter writer = new Json.createWriter(conn.getOutputStream())) {
writer.writeObject(requestBody);
}
同样,您必须将响应体解析为JSON。文档中有如下示例响应体:
{
"data": {
"token": "string"
},
"status": "string"
}

所以你想解析它并读取data/token属性:

//Getting the response code
int responsecode = conn.getResponseCode();
System.out.println("ResponseCode: " + responsecode);
if (responsecode != 200) {
throw new RuntimeException("HttpResponseCode: " + responsecode);
}
JsonObject responseBody;
try (JsonReader reader = Json.createReader(conn.getInputStream())) {
responseBody = reader.readObject();
}
String token = responseBody.getJsonObject("data").getString("token");

现在您可以为将来的请求使用令牌,如文档页面顶部所示:

url = new URL("https://api4.thetvdb.com/v4/movies?page=1");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setRequestProperty("Accept", "application/json");
responsecode = conn.getResponseCode();
if (responsecode != 200) {
throw new RuntimeException("HttpResponseCode: " + responsecode);
}
try (JsonReader reader = Json.createReader(conn.getInputStream())) {
responseBody = reader.readObject();
}
JsonArray movieEntries = responseBody.getJsonArray("data");
int count = movieEntries.size();
for (int i = 0; i < count; i++) {
JsonObject movieEntry = movieEntries.getJsonObject(i);
String movieName = movieEntry.getString("name");
System.out.println("Found movie "" + movieName + """);
}

具体的JSON属性名,如"data""name",都在文档页面中列出。单击该页面上彩色框中的任意URL路径,展开它并查看所有详细信息。

最新更新