gpt4 book ai didi

java - 如何在 Java 中使用 themoviedb.org api

转载 作者:行者123 更新时间:2023-12-02 12:34:17 24 4
gpt4 key购买 nike

我正在使用 themovidesb.org api 在 Java 程序中获取电影信息。

这是我用来将我的应用程序与 themovidesb.org 链接的函数:

        url = new URL("http://api.themoviedb.org/3/movie/550?api_key={MY_API_KEY}");
HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setDoOutput(true);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json");

BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

这是控制台中的响应:

{"adult":false,"backdrop_path":"/87hTDiay2N2qWyX4Ds7ybXi9h8I.jpg","belongs_to_collection":null,"budget":63000000,"genres":[{"id":18,"name":"Drama"}],"homepage":"http://www.foxmovies.com/movies/fight-club","id":550,"imdb_id":"tt0137523","original_language":"en","original_title":"Fight Club","overview":"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.","popularity":9.922193999999999,"poster_path":"/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg","production_companies":[{"name":"Regency Enterprises","id":508},{"name":"Fox 2000 Pictures","id":711},{"name":"Taurus Film","id":20555},{"name":"Linson Films","id":54050},{"name":"Atman Entertainment","id":54051},{"name":"Knickerbocker Films","id":54052}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"1999-10-15","revenue":100853753,"runtime":139,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"Mischief. Mayhem. Soap.","title":"Fight Club","video":false,"vote_average":8.199999999999999,"vote_count":8430}

如何解析响应以获取任何属性(例如:“original_title”、“relase-date”)并在代码中使用它?

是否存在将程序链接到数据库的替代方法(例如使用 WebResource 类)?

最佳答案

结果使用 JSON 进行格式化。有很多Java库可以用来解析它。 built-in method对于 Java EE 使用 JsonObject .

JsonReader rdr = Json.createReader(con.getInputStream());
JsonObject obj = rdr.readObject();
String title = obj.getString("original_title");

在 Android 上,您有 JSONObject .

StringBuilder responseStrBuilder = new StringBuilder();

String inputStr;
while ((inputStr = br.readLine()) != null)
responseStrBuilder.append(inputStr);
JSONObject obj = new JSONObject(responseStrBuilder.toString());
String title = obj.getString("original_title");

如果您有其他要求,还有许多其他库可以执行此操作。还有一些 HTTP 库可以轻松获取 JSON,例如 Volley对于 Android:

JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, "http://api.themoviedb.org/3/movie/550?api_key={MY_API_KEY}", null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
System.out("title: " + response.getString("original_title"));
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub

}
});

关于java - 如何在 Java 中使用 themoviedb.org api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45196758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com