all repos — tmdb-desktop-client @ b1a383bc959a9950a0630b1ec953717495199664

A simple tmdb desktop client written in Java

Update Search class and Main

Did some reformating and Refactoring
Search class will now accept spaces in the query String.
prithugoswami prithugoswami524@gmail.com
Wed, 01 May 2019 19:36:27 +0530
commit

b1a383bc959a9950a0630b1ec953717495199664

parent

b820fcafd3dc14e660da50e6017c651dab89e38c

2 files changed, 32 insertions(+), 35 deletions(-)

jump to
M src/com/oocpro/tmdbdesktop/Main.javasrc/com/oocpro/tmdbdesktop/Main.java

@@ -2,18 +2,20 @@ package com.oocpro.tmdbdesktop;

import com.oocpro.tmdbdesktop.tmdb.Movie; import com.oocpro.tmdbdesktop.tmdb.Search; + import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; public class Main{ - public static void main (String [] args)throws IOException{ + public static void main (String[] args)throws IOException{ System.out.println("Enter a movie name to search "); - BufferedReader mname = new BufferedReader(new InputStreamReader(System.in)); + BufferedReader mname = (new BufferedReader(new InputStreamReader + (System.in))); String Mov_name = mname.readLine(); Search mv = new Search(Mov_name); - Movie det = new Movie(Search.IDs.get(1)); + Movie det = new Movie(mv.movieIds.get(1)); System.out.println(det.title + " ("+det.year+")"); System.out.println("Plotline: " + det.plotline); System.out.println("Genres: " + det.genres);
M src/com/oocpro/tmdbdesktop/tmdb/Search.javasrc/com/oocpro/tmdbdesktop/tmdb/Search.java

@@ -1,45 +1,40 @@

package com.oocpro.tmdbdesktop.tmdb; import com.oocpro.tmdbdesktop.Constants; -import java.net.URL; + +import java.io.InputStream; +import java.io.IOException; import java.net.MalformedURLException; -import java.io.*; -import org.json.*; -import javax.swing.*; +import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.json.*; - public class Search{ - public static List<Integer> IDs; - public Search(String MOVIE_NAME)throws IOException,MalformedURLException{ //CONSTRUCTOR TO INITIALISE MOVIE ID - String s_url=Constants.BASEURL+"/search/movie?api_key="+Constants.API_KEY+"&language=en-US&query=%s"; //General URL format for finding a movie - String url=String.format(s_url,MOVIE_NAME); //Adding the query - System.out.println("URL = "+url); - URL us1 = new URL(url); //Creating url object - InputStream sres = (InputStream)us1.getContent(); //Storing the stream of data we get from the url - JSONObject ja = new JSONObject(new JSONTokener(sres)); - IDs = new ArrayList<Integer>(); - Iterator IDS_itr = ja.getJSONArray("results").iterator(); - //Iterating through all the results generated by the query and - //getting the IDs of each result - while(IDS_itr.hasNext()){ - JSONObject id = (JSONObject)IDS_itr.next(); - IDs.add(id.getInt("id")); - } +public class Search{ + final String SEARCH_URL = (Constants.BASEURL + "/search/movie?api_key=" + + Constants.API_KEY + "&query=%s"); + + public List<Integer> movieIds; + + public Search(String query) throws IOException,MalformedURLException { + + String url=String.format(SEARCH_URL, query); - //System.out.println(IDs.get(1) + " "); - //System.out.println(ja.toString(1)); + //replace all the spaces in the url with "%20" + url = url.replaceAll(" ", "%20"); + InputStream sres = (InputStream)new URL(url).getContent(); + JSONObject ja = new JSONObject(new JSONTokener(sres)); + movieIds = new ArrayList<Integer>(); + Iterator ids_itr = ja.getJSONArray("results").iterator(); - - //System.out.println(ja.getNames(ja)); - //System.out.println(" "+ja.getInt("total_pages")); - - + //Iterating through all the results generated by the query and + //getting the IDs of each result + while(ids_itr.hasNext()){ + JSONObject id = (JSONObject)ids_itr.next(); + movieIds.add(id.getInt("id")); } - //public static void main(String args[])throws IOException{ - // Search sc = new Search(); - //} } - +} +