2017-07-19 98 views
0

我正在使用themovidedb.org api來獲取Java程序中的電影信息。如何在Java中使用themoviedb.org api

這是我使用的鏈接與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類)?

+1

你還是使用現有的Java包裝周圍的HTTP API像https://www.themoviedb.org提到的那些/ documentation/api/wrappers-libraries - 如果你想遵循你的方法,你需要一個json解析器(或寫一個)。 PS:'setDoOutput(true);'是'POST','PUT'請求和'Content-Type'只在請求或回覆包含內容主體時相關。你可能想在你的請求中設置Accept:application/json,告訴服務器你想要什麼類型。 – zapl

回答

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庫,可以很容易地獲得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 

    } 
});