如何在Java中使用themoviedb.org api

时间:2017-07-19 17:01:14

标签: java json httpurlconnection themoviedb-api

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

这是我用于链接我的应用程序与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')并在代码中使用它?

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

2 个答案:

答案 0 :(得分:1)

使用JSON格式化结果。有许多Java库可用于解析它。 Java EE的built-in method使用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库可以很容易地为Android获取Volley之类的JSON:

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

    }
});

答案 1 :(得分:0)

创建一些变量以收集json响应并进行解析。

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

希望我能帮助您完成任务