如何在 Java 中使用 themoviedb.org api

How use themoviedb.org api in Java

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

这是我在 link 我的 themovidedb.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')并在代码中使用它?

是否存在另一种方法 link 程序到数据库(例如使用 WebResource class)?

结果使用 JSON 格式化。有很多 Java 库可以用来解析它。 built-in method for Java EE uses 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 for 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

    }
});

创建一些变量来收集 json 响应并解析它。

var resp = JSON.stringify(output)
var original_title = JSON.parse(resp).original_title
var release_date = JSON.parse(resp).release_date

希望我能帮助你完成任务